Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Start from a file #371

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,21 @@ bool ci_scroll_to_edge(arg_t dir)
return img_pan_edge(&img, dir);
}

bool ci_scroll_or_navigate(arg_t dir)
{
if (img_zoom_diff(&img, NULL) >= 0) {
arg_t n;
switch (dir) {
case DIR_LEFT: n = -1; break;
case DIR_RIGHT: n = 1; break;
default: n = 0; break;
}
return ci_navigate(n);
} else {
return ci_scroll(dir);
}
}

bool ci_drag(arg_t mode)
{
int x, y, ox, oy;
Expand Down
1 change: 1 addition & 0 deletions commands.lst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ I_CMD(navigate_frame)
I_CMD(toggle_animation)
I_CMD(scroll)
I_CMD(scroll_to_edge)
I_CMD(scroll_or_navigate)
I_CMD(drag)
I_CMD(set_zoom)
I_CMD(fit_to_win)
Expand Down
35 changes: 22 additions & 13 deletions config.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ enum {
/* levels (in percent) to use when zooming via '-' and '+':
* (first/last value is used as min/max zoom level)
*/
static const float zoom_levels[] = {
12.5, 25.0, 50.0, 75.0,
100.0, 150.0, 200.0, 400.0, 800.0
};
/* exponentials from 1 to 1500, calculated with octave:
* MAX_ZOOM = 1500;
* x = linspace(0, log(MAX_ZOOM), 300);
* y = exp(x);
* for z = y
* printf("%f, ", z);
* end
* printf("\n");
*/
static const float zoom_levels[] =
{
1.000000, 1.024761, 1.050134, 1.076136, 1.102782, 1.130087, 1.158069, 1.186743, 1.216127, 1.246239, 1.277097, 1.308718, 1.341123, 1.374330, 1.408359, 1.443230, 1.478966, 1.515585, 1.553112, 1.591568, 1.630976, 1.671360, 1.712744, 1.755152, 1.798610, 1.843145, 1.888782, 1.935549, 1.983474, 2.032586, 2.082914, 2.134488, 2.187339, 2.241499, 2.296999, 2.353874, 2.412157, 2.471884, 2.533089, 2.595809, 2.660083, 2.725948, 2.793444, 2.862611, 2.933490, 3.006125, 3.080558, 3.156834, 3.234999, 3.315099, 3.397183, 3.481299, 3.567498, 3.655831, 3.746351, 3.839112, 3.934171, 4.031583, 4.131407, 4.233703, 4.338531, 4.445955, 4.556040, 4.668849, 4.784452, 4.902918, 5.024317, 5.148721, 5.276206, 5.406848, 5.540724, 5.677915, 5.818503, 5.962572, 6.110209, 6.261500, 6.416538, 6.575415, 6.738226, 6.905067, 7.076040, 7.251247, 7.430791, 7.614781, 7.803327, 7.996542, 8.194540, 8.397441, 8.605366, 8.818439, 9.036788, 9.260543, 9.489839, 9.724812, 9.965604, 10.212357, 10.465220, 10.724344, 10.989884, 11.261999, 11.540852, 11.826610, 12.119442, 12.419526, 12.727040, 13.042167, 13.365098, 13.696025, 14.035145, 14.382662, 14.738784, 15.103724, 15.477700, 15.860936, 16.253660, 16.656109, 17.068523, 17.491148, 17.924238, 18.368051, 18.822853, 19.288917, 19.766520, 20.255949, 20.757496, 21.271462, 21.798155, 22.337888, 22.890985, 23.457778, 24.038604, 24.633812, 25.243758, 25.868806, 26.509330, 27.165715, 27.838352, 28.527643, 29.234002, 29.957851, 30.699622, 31.459760, 32.238720, 33.036967, 33.854979, 34.693245, 35.552267, 36.432559, 37.334648, 38.259073, 39.206387, 40.177157, 41.171963, 42.191402, 43.236082, 44.306629, 45.403684, 46.527902, 47.679956, 48.860536, 50.070348, 51.310115, 52.580579, 53.882501, 55.216659, 56.583851, 57.984896, 59.420631, 60.891916, 62.399630, 63.944677, 65.527979, 67.150485, 68.813165, 70.517013, 72.263050, 74.052320, 75.885893, 77.764865, 79.690363, 81.663536, 83.685567, 85.757664, 87.881067, 90.057046, 92.286904, 94.571974, 96.913624, 99.313254, 101.772301, 104.292234, 106.874562, 109.520830, 112.232621, 115.011558, 117.859302, 120.777558, 123.768071, 126.832631, 129.973071, 133.191270, 136.489153, 139.868693, 143.331912, 146.880883, 150.517727, 154.244622, 158.063797, 161.977536, 165.988182, 170.098133, 174.309848, 178.625848, 183.048714, 187.581093, 192.225695, 196.985300, 201.862756, 206.860980, 211.982962, 217.231767, 222.610535, 228.122484, 233.770912, 239.559198, 245.490804, 251.569280, 257.798262, 264.181478, 270.722744, 277.425976, 284.295183, 291.334475, 298.548064, 305.940264, 313.515500, 321.278301, 329.233314, 337.385297, 345.739127, 354.299803, 363.072444, 372.062301, 381.274751, 390.715307, 400.389615, 410.303464, 420.462784, 430.873655, 441.542304, 452.475114, 463.678626, 475.159543, 486.924733, 498.981235, 511.336262, 523.997206, 536.971641, 550.267329, 563.892226, 577.854482, 592.162450, 606.824691, 621.849977, 637.247296, 653.025860, 669.195110, 685.764719, 702.744599, 720.144910, 737.976061, 756.248720, 774.973820, 794.162563, 813.826429, 833.977181, 854.626877, 875.787870, 897.472819, 919.694699, 942.466803, 965.802757, 989.716520, 1014.222401, 1039.335059, 1065.069519, 1091.441178, 1118.465812, 1146.159589, 1174.539079, 1203.621259, 1233.423529, 1263.963717, 1295.260097, 1327.331390, 1360.196785, 1393.875943, 1428.389015, 1463.756647, 1500.000000
};

/* default slideshow delay (in sec, overwritten via -S option): */
enum { SLIDESHOW_DELAY = 5 };
Expand All @@ -32,7 +41,7 @@ static const double GAMMA_MAX = 10.0;
static const int GAMMA_RANGE = 32;

/* command i_scroll pans image 1/PAN_FRACTION of screen width/height */
static const int PAN_FRACTION = 5;
static const int PAN_FRACTION = 70;

/* if false, pixelate images at zoom level != 100%,
* toggled with 'a' key binding
Expand Down Expand Up @@ -112,14 +121,14 @@ static const keymap_t keys[] = {
{ ControlMask, XK_n, i_navigate_frame, +1 },
{ ControlMask, XK_p, i_navigate_frame, -1 },
{ ControlMask, XK_space, i_toggle_animation, None },
{ 0, XK_h, i_scroll, DIR_LEFT },
{ 0, XK_Left, i_scroll, DIR_LEFT },
{ 0, XK_j, i_scroll, DIR_DOWN },
{ 0, XK_Down, i_scroll, DIR_DOWN },
{ 0, XK_k, i_scroll, DIR_UP },
{ 0, XK_Up, i_scroll, DIR_UP },
{ 0, XK_l, i_scroll, DIR_RIGHT },
{ 0, XK_Right, i_scroll, DIR_RIGHT },
{ 0, XK_h, i_scroll_or_navigate, DIR_LEFT },
{ 0, XK_Left, i_scroll_or_navigate, DIR_LEFT },
{ 0, XK_j, i_scroll_or_navigate, DIR_DOWN },
{ 0, XK_Down, i_scroll_or_navigate, DIR_DOWN },
{ 0, XK_k, i_scroll_or_navigate, DIR_UP },
{ 0, XK_Up, i_scroll_or_navigate, DIR_UP },
{ 0, XK_l, i_scroll_or_navigate, DIR_RIGHT },
{ 0, XK_Right, i_scroll_or_navigate, DIR_RIGHT },
{ 0, XK_H, i_scroll_to_edge, DIR_LEFT },
{ 0, XK_J, i_scroll_to_edge, DIR_DOWN },
{ 0, XK_K, i_scroll_to_edge, DIR_UP },
Expand Down
19 changes: 14 additions & 5 deletions image.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,10 @@ void img_check_pan(img_t *img, bool moved)
img->dirty = true;
}

bool img_fit(img_t *img)
int img_zoom_diff(img_t *img, float *zptr)
{
float z, zw, zh;

if (img->scalemode == SCALE_ZOOM)
return false;

zw = (float) img->win->w / (float) img->w;
zh = (float) img->win->h / (float) img->h;

Expand All @@ -413,8 +410,20 @@ bool img_fit(img_t *img)
break;
}
z = MIN(z, img->scalemode == SCALE_DOWN ? 1.0 : zoom_max);
if (zptr != NULL)
*zptr = z;

if (zoomdiff(img, z) != 0) {
return zoomdiff(img, z);
}

bool img_fit(img_t *img)
{
float z;

if (img->scalemode == SCALE_ZOOM)
return false;

if (img_zoom_diff(img, &z) != 0) {
img->zoom = z;
img->dirty = true;
return true;
Expand Down
8 changes: 8 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,14 @@ int main(int argc, char **argv)
filecnt = fileidx;
fileidx = options->startnum < filecnt ? options->startnum : 0;

if (options->startfile != NULL) {
for (int i = 0; i < filecnt; ++i) {
if (strcmp(options->startfile, files[i].path) == 0) {
fileidx = i;
}
}
}

for (i = 0; i < ARRLEN(buttons); i++) {
if (buttons[i].cmd == i_cursor_navigate) {
imgcursor[0] = CURSOR_LEFT;
Expand Down
8 changes: 6 additions & 2 deletions options.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void print_usage(void)
{
printf("usage: sxiv [-abcfhiopqrtvZ] [-A FRAMERATE] [-e WID] [-G GAMMA] "
"[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] [-z ZOOM] "
"FILES...\n");
"[-F FILE] FILES...\n");
}

void print_version(void)
Expand All @@ -53,6 +53,7 @@ void parse_options(int argc, char **argv)
_options.to_stdout = false;
_options.recursive = false;
_options.startnum = 0;
_options.startfile = NULL;

_options.scalemode = SCALE_DOWN;
_options.zoom = 1.0;
Expand All @@ -72,7 +73,7 @@ void parse_options(int argc, char **argv)
_options.clean_cache = false;
_options.private_mode = false;

while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:N:opqrS:s:tvZz:")) != -1) {
while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:F:N:opqrS:s:tvZz:")) != -1) {
switch (opt) {
case '?':
print_usage();
Expand Down Expand Up @@ -122,6 +123,9 @@ void parse_options(int argc, char **argv)
error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg);
_options.startnum = n - 1;
break;
case 'F':
_options.startfile = optarg;
break;
case 'N':
_options.res_name = optarg;
break;
Expand Down
5 changes: 5 additions & 0 deletions sxiv.1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ sxiv \- Simple X Image Viewer
.IR NAME ]
.RB [ \-n
.IR NUM ]
.RB [ \-F
.IR FILE ]
.RB [ \-S
.IR DELAY ]
.RB [ \-s
Expand Down Expand Up @@ -67,6 +69,9 @@ Set the resource name of sxiv's X window to NAME.
.BI "\-n " NUM
Start at picture number NUM.
.TP
.BI "\-F " FILE
Start at picture file FILE.
.TP
.B \-h
Print brief usage information to standard output and exit.
.TP
Expand Down
2 changes: 2 additions & 0 deletions sxiv.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ void img_toggle_antialias(img_t*);
bool img_change_gamma(img_t*, int);
bool img_frame_navigate(img_t*, int);
bool img_frame_animate(img_t*);
int img_zoom_diff(img_t*, float*);


/* options.c */
Expand All @@ -264,6 +265,7 @@ struct opt {
bool recursive;
int filecnt;
int startnum;
char *startfile;

/* image: */
scalemode_t scalemode;
Expand Down
5 changes: 1 addition & 4 deletions window.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,7 @@ void win_draw_bar(win_t *win)
d = XftDrawCreate(e->dpy, win->buf.pm, DefaultVisual(e->dpy, e->scr),
DefaultColormap(e->dpy, e->scr));

if (win->fullscreen && !win->light)
bg = &win->bg, fg = &win->fg;
else
bg = &win->fg, fg = &win->bg;
bg = &win->bg, fg = &win->fg;

XSetForeground(e->dpy, gc, bg->pixel);
XFillRectangle(e->dpy, win->buf.pm, gc, 0, win->h, win->w, win->bar.h);
Expand Down