2011-08-19 15:02:10 +02:00
|
|
|
/* sxiv: commands.c
|
2011-07-26 18:01:29 +02:00
|
|
|
* Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
|
|
|
|
*
|
2011-08-18 01:18:26 +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-07-26 18:01:29 +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-07-26 18:01:29 +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
|
2011-08-18 01:18:26 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
|
2011-07-26 18:01:29 +02:00
|
|
|
*/
|
|
|
|
|
2011-09-03 15:58:58 +02:00
|
|
|
#define _POSIX_C_SOURCE 200112L /* for setenv(3) */
|
2011-07-26 18:01:29 +02:00
|
|
|
#include <stdlib.h>
|
2011-09-03 15:58:58 +02:00
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2011-08-19 15:02:10 +02:00
|
|
|
#include "commands.h"
|
2011-07-26 18:01:29 +02:00
|
|
|
#include "image.h"
|
|
|
|
#include "thumbs.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
void cleanup();
|
|
|
|
void remove_file(int, unsigned char);
|
|
|
|
void load_image(int);
|
2011-09-02 04:33:44 +02:00
|
|
|
void redraw();
|
2011-09-03 17:01:39 +02:00
|
|
|
void reset_cursor();
|
2011-09-02 04:33:44 +02:00
|
|
|
void animate();
|
|
|
|
void set_timeout(timeout_f, int, int);
|
|
|
|
void reset_timeout(timeout_f);
|
2011-07-26 18:01:29 +02:00
|
|
|
|
|
|
|
extern appmode_t mode;
|
|
|
|
extern img_t img;
|
|
|
|
extern tns_t tns;
|
|
|
|
extern win_t win;
|
|
|
|
|
2011-08-18 00:38:55 +02:00
|
|
|
extern fileinfo_t *files;
|
2011-07-26 18:01:29 +02:00
|
|
|
extern int filecnt, fileidx;
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int it_quit(arg_t a) {
|
2011-07-26 18:01:29 +02:00
|
|
|
cleanup();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int it_switch_mode(arg_t a) {
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE) {
|
2011-08-19 13:06:19 +02:00
|
|
|
if (!tns.thumbs)
|
|
|
|
tns_init(&tns, filecnt);
|
|
|
|
img_close(&img, 0);
|
2011-09-03 17:01:39 +02:00
|
|
|
reset_timeout(reset_cursor);
|
2011-08-19 13:06:19 +02:00
|
|
|
tns.sel = fileidx;
|
|
|
|
tns.dirty = 1;
|
2011-08-19 13:09:22 +02:00
|
|
|
mode = MODE_THUMB;
|
2011-07-26 18:01:29 +02:00
|
|
|
} else {
|
2011-08-19 13:06:19 +02:00
|
|
|
load_image(tns.sel);
|
2011-08-19 13:09:22 +02:00
|
|
|
mode = MODE_IMAGE;
|
2011-07-26 18:01:29 +02:00
|
|
|
}
|
2011-08-19 13:06:19 +02:00
|
|
|
return 1;
|
2011-07-26 18:01:29 +02:00
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int it_toggle_fullscreen(arg_t a) {
|
2011-07-26 18:01:29 +02:00
|
|
|
win_toggle_fullscreen(&win);
|
2011-09-02 04:33:44 +02:00
|
|
|
set_timeout(redraw, TO_REDRAW_RESIZE, 0);
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE)
|
2011-07-26 18:01:29 +02:00
|
|
|
img.checkpan = 1;
|
|
|
|
else
|
|
|
|
tns.dirty = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int it_reload_image(arg_t a) {
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE) {
|
2011-08-19 13:06:19 +02:00
|
|
|
load_image(fileidx);
|
2011-09-03 17:01:39 +02:00
|
|
|
} else {
|
|
|
|
win_set_cursor(&win, CURSOR_WATCH);
|
|
|
|
if (!tns_load(&tns, tns.sel, &files[tns.sel], True, False)) {
|
|
|
|
remove_file(tns.sel, 0);
|
|
|
|
tns.dirty = 1;
|
|
|
|
if (tns.sel >= tns.cnt)
|
|
|
|
tns.sel = tns.cnt - 1;
|
|
|
|
}
|
2011-07-26 18:01:29 +02:00
|
|
|
}
|
2011-08-19 13:06:19 +02:00
|
|
|
return 1;
|
2011-07-26 18:01:29 +02:00
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int it_remove_image(arg_t a) {
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE) {
|
2011-08-19 13:06:19 +02:00
|
|
|
remove_file(fileidx, 1);
|
|
|
|
load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
|
2011-07-26 18:01:29 +02:00
|
|
|
return 1;
|
2011-08-19 13:06:19 +02:00
|
|
|
} else if (tns.sel < tns.cnt) {
|
|
|
|
remove_file(tns.sel, 1);
|
2011-07-26 18:01:29 +02:00
|
|
|
tns.dirty = 1;
|
2011-08-19 13:06:19 +02:00
|
|
|
if (tns.sel >= tns.cnt)
|
|
|
|
tns.sel = tns.cnt - 1;
|
|
|
|
return 1;
|
2011-07-26 18:01:29 +02:00
|
|
|
} else {
|
2011-08-19 13:06:19 +02:00
|
|
|
return 0;
|
2011-07-26 18:01:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int i_navigate(arg_t a) {
|
2011-09-03 23:07:14 +02:00
|
|
|
long n = (long) a;
|
2011-08-12 17:14:14 +02:00
|
|
|
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE) {
|
2011-07-26 18:01:29 +02:00
|
|
|
n += fileidx;
|
|
|
|
if (n < 0)
|
|
|
|
n = 0;
|
|
|
|
if (n >= filecnt)
|
|
|
|
n = filecnt - 1;
|
|
|
|
|
|
|
|
if (n != fileidx) {
|
|
|
|
load_image(n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int it_first(arg_t a) {
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE && fileidx != 0) {
|
2011-07-26 18:01:29 +02:00
|
|
|
load_image(0);
|
|
|
|
return 1;
|
2011-08-19 13:09:22 +02:00
|
|
|
} else if (mode == MODE_THUMB && tns.sel != 0) {
|
2011-07-26 18:01:29 +02:00
|
|
|
tns.sel = 0;
|
|
|
|
tns.dirty = 1;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int it_last(arg_t a) {
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE && fileidx != filecnt - 1) {
|
2011-07-26 18:01:29 +02:00
|
|
|
load_image(filecnt - 1);
|
|
|
|
return 1;
|
2011-08-19 13:09:22 +02:00
|
|
|
} else if (mode == MODE_THUMB && tns.sel != tns.cnt - 1) {
|
2011-07-26 18:01:29 +02:00
|
|
|
tns.sel = tns.cnt - 1;
|
|
|
|
tns.dirty = 1;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-17 00:56:18 +02:00
|
|
|
int i_navigate_frame(arg_t a) {
|
2011-08-19 18:46:17 +02:00
|
|
|
if (mode == MODE_IMAGE && !img.multi.animate)
|
2011-09-03 23:07:14 +02:00
|
|
|
return img_frame_navigate(&img, (long) a);
|
2011-08-19 18:46:17 +02:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i_toggle_animation(arg_t a) {
|
2011-09-02 04:33:44 +02:00
|
|
|
int delay;
|
|
|
|
|
2011-08-19 18:46:17 +02:00
|
|
|
if (mode != MODE_IMAGE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (img.multi.animate) {
|
2011-09-02 04:33:44 +02:00
|
|
|
reset_timeout(animate);
|
2011-08-19 18:46:17 +02:00
|
|
|
img.multi.animate = 0;
|
|
|
|
return 0;
|
|
|
|
} else {
|
2011-09-02 04:33:44 +02:00
|
|
|
delay = img_frame_animate(&img, 1);
|
|
|
|
set_timeout(animate, delay, 1);
|
2011-08-19 18:46:17 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2011-08-17 00:56:18 +02:00
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int it_move(arg_t a) {
|
2011-08-12 17:14:14 +02:00
|
|
|
direction_t dir = (direction_t) a;
|
|
|
|
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE)
|
2011-07-26 18:01:29 +02:00
|
|
|
return img_pan(&img, &win, dir, 0);
|
|
|
|
else
|
|
|
|
return tns_move_selection(&tns, &win, dir);
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int i_pan_screen(arg_t a) {
|
2011-08-12 17:14:14 +02:00
|
|
|
direction_t dir = (direction_t) a;
|
|
|
|
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE)
|
2011-07-26 18:01:29 +02:00
|
|
|
return img_pan(&img, &win, dir, 1);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int i_pan_edge(arg_t a) {
|
2011-08-12 17:14:14 +02:00
|
|
|
direction_t dir = (direction_t) a;
|
|
|
|
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE)
|
2011-07-26 18:01:29 +02:00
|
|
|
return img_pan_edge(&img, &win, dir);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
/* Xlib helper function for i_drag() */
|
2011-08-12 17:14:14 +02:00
|
|
|
Bool is_motionnotify(Display *d, XEvent *e, XPointer a) {
|
2011-08-12 12:27:07 +02:00
|
|
|
return e != NULL && e->type == MotionNotify;
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int i_drag(arg_t a) {
|
2011-08-12 12:27:07 +02:00
|
|
|
int dx = 0, dy = 0, i, ox, oy, x, y;
|
|
|
|
unsigned int ui;
|
|
|
|
Bool dragging = True, next = False;
|
|
|
|
XEvent e;
|
|
|
|
Window w;
|
|
|
|
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode != MODE_IMAGE)
|
2011-08-12 12:27:07 +02:00
|
|
|
return 0;
|
|
|
|
if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
win_set_cursor(&win, CURSOR_HAND);
|
|
|
|
|
|
|
|
while (dragging) {
|
|
|
|
if (!next)
|
|
|
|
XMaskEvent(win.env.dpy,
|
|
|
|
ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
|
|
|
|
switch (e.type) {
|
|
|
|
case ButtonPress:
|
|
|
|
case ButtonRelease:
|
|
|
|
dragging = False;
|
|
|
|
break;
|
|
|
|
case MotionNotify:
|
|
|
|
x = e.xmotion.x;
|
|
|
|
y = e.xmotion.y;
|
|
|
|
if (x >= 0 && x <= win.w && y >= 0 && y <= win.h) {
|
|
|
|
dx += x - ox;
|
|
|
|
dy += y - oy;
|
|
|
|
}
|
|
|
|
ox = x;
|
|
|
|
oy = y;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (dragging)
|
2011-08-12 17:14:14 +02:00
|
|
|
next = XCheckIfEvent(win.env.dpy, &e, is_motionnotify, None);
|
2011-08-12 12:27:07 +02:00
|
|
|
if ((!dragging || !next) && (dx != 0 || dy != 0)) {
|
|
|
|
if (img_move(&img, &win, dx, dy))
|
|
|
|
img_render(&img, &win);
|
|
|
|
dx = dy = 0;
|
|
|
|
}
|
2011-07-26 18:01:29 +02:00
|
|
|
}
|
2011-08-12 12:27:07 +02:00
|
|
|
|
|
|
|
win_set_cursor(&win, CURSOR_ARROW);
|
2011-09-03 17:01:39 +02:00
|
|
|
set_timeout(reset_cursor, TO_CURSOR_HIDE, 1);
|
2011-09-02 04:33:44 +02:00
|
|
|
reset_timeout(redraw);
|
2011-08-12 12:27:07 +02:00
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int i_zoom(arg_t a) {
|
2011-09-03 23:07:14 +02:00
|
|
|
long scale = (long) a;
|
2011-08-12 17:14:14 +02:00
|
|
|
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode != MODE_IMAGE)
|
2011-07-26 18:01:29 +02:00
|
|
|
return 0;
|
2011-08-19 18:46:17 +02:00
|
|
|
|
2011-07-26 18:01:29 +02:00
|
|
|
if (scale > 0)
|
|
|
|
return img_zoom_in(&img, &win);
|
|
|
|
else if (scale < 0)
|
|
|
|
return img_zoom_out(&img, &win);
|
|
|
|
else
|
|
|
|
return img_zoom(&img, &win, 1.0);
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int i_fit_to_win(arg_t a) {
|
2011-08-12 17:14:14 +02:00
|
|
|
int ret;
|
|
|
|
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE) {
|
2011-07-26 18:01:29 +02:00
|
|
|
if ((ret = img_fit_win(&img, &win)))
|
|
|
|
img_center(&img, &win);
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int i_fit_to_img(arg_t a) {
|
2011-08-12 17:14:14 +02:00
|
|
|
int ret, x, y;
|
2011-07-26 18:01:29 +02:00
|
|
|
unsigned int w, h;
|
|
|
|
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE) {
|
2011-07-26 18:01:29 +02:00
|
|
|
x = MAX(0, win.x + img.x);
|
|
|
|
y = MAX(0, win.y + img.y);
|
|
|
|
w = img.w * img.zoom;
|
|
|
|
h = img.h * img.zoom;
|
|
|
|
if ((ret = win_moveresize(&win, x, y, w, h))) {
|
|
|
|
img.x = x - win.x;
|
|
|
|
img.y = y - win.y;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2011-08-12 17:14:14 +02:00
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int i_rotate(arg_t a) {
|
|
|
|
direction_t dir = (direction_t) a;
|
|
|
|
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE) {
|
2011-08-19 13:06:19 +02:00
|
|
|
if (dir == DIR_LEFT) {
|
|
|
|
img_rotate_left(&img, &win);
|
|
|
|
return 1;
|
|
|
|
} else if (dir == DIR_RIGHT) {
|
|
|
|
img_rotate_right(&img, &win);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i_toggle_antialias(arg_t a) {
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE) {
|
2011-08-19 13:06:19 +02:00
|
|
|
img_toggle_antialias(&img);
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int i_toggle_alpha(arg_t a) {
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE) {
|
2011-08-19 13:06:19 +02:00
|
|
|
img.alpha ^= 1;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int it_open_with(arg_t a) {
|
2011-08-12 17:14:14 +02:00
|
|
|
const char *prog = (const char*) a;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
if (!prog || !*prog)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if((pid = fork()) == 0) {
|
|
|
|
execlp(prog, prog,
|
2011-08-19 13:09:22 +02:00
|
|
|
files[mode == MODE_IMAGE ? fileidx : tns.sel].path, NULL);
|
2011-08-12 17:14:14 +02:00
|
|
|
warn("could not exec: %s", prog);
|
|
|
|
exit(1);
|
|
|
|
} else if (pid < 0) {
|
2011-09-03 15:58:58 +02:00
|
|
|
warn("could not fork. program was: %s", prog);
|
2011-08-12 17:14:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:06:19 +02:00
|
|
|
int it_shell_cmd(arg_t a) {
|
2011-09-03 15:58:58 +02:00
|
|
|
int n, status;
|
|
|
|
const char *cmdline = (const char*) a;
|
2011-08-12 17:14:14 +02:00
|
|
|
pid_t pid;
|
|
|
|
|
2011-09-03 15:58:58 +02:00
|
|
|
if (!cmdline || !*cmdline)
|
2011-08-12 17:14:14 +02:00
|
|
|
return 0;
|
|
|
|
|
2011-09-03 15:58:58 +02:00
|
|
|
n = mode == MODE_IMAGE ? fileidx : tns.sel;
|
|
|
|
|
|
|
|
if (setenv("SXIV_IMG", files[n].path, 1) < 0) {
|
|
|
|
warn("could not change env.-variable: SXIV_IMG. command line was: %s",
|
|
|
|
cmdline);
|
2011-08-12 17:14:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pid = fork()) == 0) {
|
|
|
|
execl("/bin/sh", "/bin/sh", "-c", cmdline, NULL);
|
|
|
|
warn("could not exec: /bin/sh");
|
|
|
|
exit(1);
|
|
|
|
} else if (pid < 0) {
|
|
|
|
warn("could not fork. command line was: %s", cmdline);
|
2011-09-03 17:01:39 +02:00
|
|
|
return 0;
|
2011-08-12 17:14:14 +02:00
|
|
|
}
|
|
|
|
|
2011-09-03 17:01:39 +02:00
|
|
|
win_set_cursor(&win, CURSOR_WATCH);
|
|
|
|
|
2011-08-12 17:14:14 +02:00
|
|
|
waitpid(pid, &status, 0);
|
|
|
|
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
|
|
|
|
warn("child exited with non-zero return value: %d. command line was: %s",
|
|
|
|
WEXITSTATUS(status), cmdline);
|
|
|
|
|
2011-08-19 13:09:22 +02:00
|
|
|
if (mode == MODE_IMAGE) {
|
2011-08-12 17:14:14 +02:00
|
|
|
img_close(&img, 1);
|
|
|
|
load_image(fileidx);
|
2011-09-03 15:58:58 +02:00
|
|
|
}
|
|
|
|
if (!tns_load(&tns, n, &files[n], True, mode == MODE_IMAGE) &&
|
|
|
|
mode == MODE_THUMB)
|
|
|
|
{
|
|
|
|
remove_file(tns.sel, 0);
|
|
|
|
tns.dirty = 1;
|
|
|
|
if (tns.sel >= tns.cnt)
|
|
|
|
tns.sel = tns.cnt - 1;
|
2011-08-12 17:14:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|