Krolyxon
Commit 8123d63

Branches

Refs →

nsxiv8123d63

Added plenty lots of options

Author
Bert
Date
2011-01-28 13:34 +0100
Parent
17e2a79
Commit
8123d63c5b76909ce70f09614ac0e45760a69d71

+126 −34

10 files changed

  1. M Makefile +1 −1
  2. M README.md +10 −2
  3. M config.h +3 −1
  4. M image.c +36 −22
  5. M image.h +5 −2
  6. M options.c +34 −2
  7. M options.h +8 −0
  8. M sxiv.1 +24 −3
  9. M window.c +4 −0
  10. M window.h +1 −1
M Makefile View
@@ -1,6 +1,6 @@
11 all: sxiv
22
3−VERSION=git-20110127
3+VERSION=git-20110128
44
55 CC?=gcc
66 PREFIX?=/usr/local
M README.md View
@@ -30,8 +30,16 @@ Usage
3030 -----
3131 sxiv supports the following command-line options:
3232
33− -w WIDTHxHEIGHT set window width to WIDTH and height to HEIGHT
34− (if HEIGHT is omitted, height is also set to WIDTH)
33+ -d scale all images to 100%, but fit large images into window
34+ -f start in fullscreen mode
35+ -p pixelize, i.e. turn off image anti-aliasing
36+ -s scale all images to fit into window
37+ -v print version information and exit
38+ -w WIDTHxHEIGHT
39+ set window width to WIDTH and height to HEIGHT
40+ (if HEIGHT is omitted, height is also set to WIDTH)
41+ -Z same as `-z 100'
42+ -z ZOOM scale all images to current zoom level, use ZOOM at startup
3543
3644 Use the following keys to control sxiv:
3745
M config.h View
@@ -1,4 +1,5 @@
1−/* default window dimensions: */
1+/* default window dimensions: *
2+ * (also controllable via -w option) */
23 #define WIN_WIDTH 800
34 #define WIN_HEIGHT 600
45
@@ -7,6 +8,7 @@
78 #define BG_COLOR "#888888"
89
910 /* how should images be scaled when they are loaded?: *
11+ * (also controllable via -d/-s/-Z/-z options) *
1012 * SCALE_DOWN: 100%, but fit large images into window, *
1113 * SCALE_FIT: fit all images into window, *
1214 * SCALE_ZOOM: use current zoom level, 100% at startup */
M image.c View
@@ -23,6 +23,7 @@
2323
2424 #include "sxiv.h"
2525 #include "image.h"
26+#include "options.h"
2627
2728 int zl_cnt;
2829 float zoom_min;
@@ -34,8 +35,10 @@ void img_init(img_t *img, win_t *win) {
3435 zoom_max = zoom_levels[zl_cnt - 1] / 100.0;
3536
3637 if (img) {
37− img->zoom = 1.0;
38− img->aa = 1;
38+ img->zoom = options->zoom;
39+ img->zoom = MAX(img->zoom, zoom_min);
40+ img->zoom = MIN(img->zoom, zoom_max);
41+ img->aa = options->aa;
3942 }
4043
4144 if (win) {
@@ -102,32 +105,20 @@ void img_check_pan(img_t *img, win_t *win) {
102105 void img_render(img_t *img, win_t *win) {
103106 int sx, sy, sw, sh;
104107 int dx, dy, dw, dh;
105− float zw, zh;
106108
107109 if (!img || !win || !imlib_context_get_image())
108110 return;
109111
110− if (!img->zoomed && SCALE_MODE != SCALE_ZOOM) {
111− /* set zoom level to fit image into window */
112− zw = (float) win->w / (float) img->w;
113− zh = (float) win->h / (float) img->h;
114− img->zoom = MIN(zw, zh);
115−
116− if (img->zoom < zoom_min)
117− img->zoom = zoom_min;
118− else if (img->zoom > zoom_max)
119− img->zoom = zoom_max;
120−
121− if (SCALE_MODE == SCALE_DOWN && img->zoom > 1.0)
112+ if (!img->zoomed && options->scalemode != SCALE_ZOOM) {
113+ img_fit(img, win);
114+ if (options->scalemode == SCALE_DOWN && img->zoom > 1.0)
122115 img->zoom = 1.0;
123116 }
124117
125118 if (!img->re) {
126119 /* rendered for the first time */
127120 img->re = 1;
128− /* center image in window */
129− img->x = (win->w - img->w * img->zoom) / 2;
130− img->y = (win->h - img->h * img->zoom) / 2;
121+ img_center(img, win);
131122 }
132123
133124 if (img->checkpan) {
@@ -167,14 +158,37 @@ void img_render(img_t *img, win_t *win) {
167158 win_draw(win);
168159 }
169160
161+int img_fit(img_t *img, win_t *win) {
162+ float oz, zw, zh;
163+
164+ if (!img || !win)
165+ return 0;
166+
167+ oz = img->zoom;
168+ zw = (float) win->w / (float) img->w;
169+ zh = (float) win->h / (float) img->h;
170+
171+ img->zoom = MIN(zw, zh);
172+ img->zoom = MAX(img->zoom, zoom_min);
173+ img->zoom = MIN(img->zoom, zoom_max);
174+
175+ return oz != img->zoom;
176+}
177+
178+void img_center(img_t *img, win_t *win) {
179+ if (!img || !win)
180+ return;
181+
182+ img->x = (win->w - img->w * img->zoom) / 2;
183+ img->y = (win->h - img->h * img->zoom) / 2;
184+}
185+
170186 int img_zoom(img_t *img, float z) {
171187 if (!img)
172188 return 0;
173189
174− if (z < zoom_min)
175− z = zoom_min;
176− else if (z > zoom_max)
177− z = zoom_max;
190+ z = MAX(z, zoom_min);
191+ z = MIN(z, zoom_max);
178192
179193 if (z != img->zoom) {
180194 img->x -= (img->w * z - img->w * img->zoom) / 2;
M image.h View
@@ -21,11 +21,11 @@
2121
2222 #include "window.h"
2323
24−enum scalemode {
24+typedef enum scalemode_e {
2525 SCALE_DOWN = 0,
2626 SCALE_FIT,
2727 SCALE_ZOOM
28−};
28+} scalemode_t;
2929
3030 typedef enum pandir_e {
3131 PAN_LEFT = 0,
@@ -52,6 +52,9 @@ void img_free(img_t*);
5252 int img_load(img_t*, const char*);
5353 void img_render(img_t*, win_t*);
5454
55+int img_fit(img_t*, win_t*);
56+void img_center(img_t*, win_t*);
57+
5558 int img_zoom_in(img_t*);
5659 int img_zoom_out(img_t*);
5760
M options.c View
@@ -29,7 +29,7 @@ options_t _options;
2929 const options_t *options = (const options_t*) &_options;
3030
3131 void print_usage() {
32− printf("usage: sxiv [-hv] [-w WIDTH[xHEIGHT]] FILES...\n");
32+ printf("usage: sxiv [-dfhpsvZ] [-w WIDTH[xHEIGHT]] [-z ZOOM] FILES...\n");
3333 }
3434
3535 void print_version() {
@@ -39,19 +39,37 @@ void print_version() {
3939
4040 void parse_options(int argc, char **argv) {
4141 unsigned short w, h;
42+ float z;
4243 int opt;
4344
45+ _options.scalemode = SCALE_MODE;
46+ _options.zoom = 1.0;
47+ _options.aa = 1;
48+
4449 _options.winw = w = 0;
4550 _options.winh = h = 0;
51+ _options.fullscreen = 0;
4652
47− while ((opt = getopt(argc, argv, "hvw:")) != -1) {
53+ while ((opt = getopt(argc, argv, "dfhpsvw:Zz:")) != -1) {
4854 switch (opt) {
4955 case '?':
5056 print_usage();
5157 exit(1);
58+ case 'd':
59+ _options.scalemode = SCALE_DOWN;
60+ break;
61+ case 'f':
62+ _options.fullscreen = 1;
63+ break;
5264 case 'h':
5365 print_usage();
5466 exit(0);
67+ case 'p':
68+ _options.aa = 0;
69+ break;
70+ case 's':
71+ _options.scalemode = SCALE_FIT;
72+ break;
5573 case 'v':
5674 print_version();
5775 exit(0);
@@ -65,6 +83,20 @@ void parse_options(int argc, char **argv) {
6583 _options.winh = (int) h;
6684 }
6785 break;
86+ case 'Z':
87+ _options.scalemode = SCALE_ZOOM;
88+ _options.zoom = 1.0;
89+ break;
90+ case 'z':
91+ _options.scalemode = SCALE_ZOOM;
92+ if (!sscanf(optarg, "%f", &z) || z < 0) {
93+ fprintf(stderr, "sxiv: invalid argument for option -z: %s\n",
94+ optarg);
95+ exit(1);
96+ } else {
97+ _options.zoom = z / 100.0;
98+ }
99+ break;
68100 }
69101 }
70102
M options.h View
@@ -19,11 +19,19 @@
1919 #ifndef OPTIONS_H
2020 #define OPTIONS_H
2121
22+#include "image.h"
23+
2224 typedef struct options_s {
2325 const char **filenames;
2426 int filecnt;
27+
28+ scalemode_t scalemode;
29+ float zoom;
30+ unsigned char aa;
31+
2532 int winw;
2633 int winh;
34+ unsigned char fullscreen;
2735 } options_t;
2836
2937 extern const options_t *options;
M sxiv.1 View
@@ -3,11 +3,12 @@
33 sxiv \- Simple (or small or suckless) X Image Viewer
44 .SH SYNOPSIS
55 .B sxiv
6−.RB [ \-hv ]
7−[
8−.B \-w
6+.RB [ \-dfhpsvZ ]
7+.RB [ \-w
98 .IB WIDTH x HEIGHT
109 ]
10+.RB [ \-z
11+.IR ZOOM ]
1112 .IR FILE ...
1213 .SH DESCRIPTION
1314 sxiv is a simple image viewer for X. It only has the most basic features
@@ -17,9 +18,21 @@ Please note, that the fullscreen mode requires an EWMH/NetWM compliant window
1718 manager.
1819 .SH OPTIONS
1920 .TP
21+.B \-d
22+Scale all images to 100%, but fit large images into window.
23+.TP
24+.B \-f
25+Start in fullscreen mode.
26+.TP
2027 .B \-h
2128 Print brief usage information to standard output and exit.
2229 .TP
30+.B \-p
31+Pixelize images, i.e. turn off anti-aliasing.
32+.TP
33+.B \-s
34+Scale all images to fit into window.
35+.TP
2336 .B \-v
2437 Print version information to standard output and exit.
2538 .TP
@@ -32,6 +45,14 @@ If
3245 .I HEIGHT
3346 is omitted, height is also set to
3447 .IR WIDTH .
48+.TP
49+.B \-Z
50+The same as `-z 100'.
51+.TP
52+.BI "\-z " ZOOM
53+Scale all images to the current zoom level, use a zoom level of
54+.I ZOOM
55+at startup.
3556 .SH KEYBOARD COMMANDS
3657 .SS General
3758 .TP
M window.c View
@@ -54,6 +54,7 @@ void win_open(win_t *win) {
5454 win->bgcol = bgcol.pixel;
5555 win->pm = 0;
5656
57+ win->fullscreen = 0;
5758 win->w = MIN(options->winw, e->scrw);
5859 win->h = MIN(options->winh, e->scrh);
5960 win->x = (e->scrw - win->w) / 2;
@@ -81,6 +82,9 @@ void win_open(win_t *win) {
8182
8283 XMapWindow(e->dpy, win->xwin);
8384 XFlush(e->dpy);
85+
86+ if (options->fullscreen)
87+ win_toggle_fullscreen(win);
8488 }
8589
8690 void win_close(win_t *win) {
M window.h View
@@ -45,7 +45,7 @@ typedef struct win_s {
4545 int y;
4646
4747 int bw;
48− int fullscreen;
48+ unsigned char fullscreen;
4949 } win_t;
5050
5151 void win_open(win_t*);