nsxiv8123d63
Added plenty lots of options
+126 −34
10 files changed
- M Makefile +1 −1
- M README.md +10 −2
- M config.h +3 −1
- M image.c +36 −22
- M image.h +5 −2
- M options.c +34 −2
- M options.h +8 −0
- M sxiv.1 +24 −3
- M window.c +4 −0
- M window.h +1 −1
| @@ -1,6 +1,6 @@ | |||
| 1 | 1 | all: sxiv | |
| 2 | 2 | ||
| 3 | − | VERSION=git-20110127 | |
| 3 | + | VERSION=git-20110128 | |
| 4 | 4 | ||
| 5 | 5 | CC?=gcc | |
| 6 | 6 | PREFIX?=/usr/local | |
| @@ -30,8 +30,16 @@ Usage | |||
| 30 | 30 | ----- | |
| 31 | 31 | sxiv supports the following command-line options: | |
| 32 | 32 | ||
| 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 | |
| 35 | 43 | ||
| 36 | 44 | Use the following keys to control sxiv: | |
| 37 | 45 | ||
| @@ -1,4 +1,5 @@ | |||
| 1 | − | /* default window dimensions: */ | |
| 1 | + | /* default window dimensions: * | |
| 2 | + | * (also controllable via -w option) */ | |
| 2 | 3 | #define WIN_WIDTH 800 | |
| 3 | 4 | #define WIN_HEIGHT 600 | |
| 4 | 5 | ||
| @@ -7,6 +8,7 @@ | |||
| 7 | 8 | #define BG_COLOR "#888888" | |
| 8 | 9 | ||
| 9 | 10 | /* how should images be scaled when they are loaded?: * | |
| 11 | + | * (also controllable via -d/-s/-Z/-z options) * | |
| 10 | 12 | * SCALE_DOWN: 100%, but fit large images into window, * | |
| 11 | 13 | * SCALE_FIT: fit all images into window, * | |
| 12 | 14 | * SCALE_ZOOM: use current zoom level, 100% at startup */ | |
| @@ -23,6 +23,7 @@ | |||
| 23 | 23 | ||
| 24 | 24 | #include "sxiv.h" | |
| 25 | 25 | #include "image.h" | |
| 26 | + | #include "options.h" | |
| 26 | 27 | ||
| 27 | 28 | int zl_cnt; | |
| 28 | 29 | float zoom_min; | |
| @@ -34,8 +35,10 @@ void img_init(img_t *img, win_t *win) { | |||
| 34 | 35 | zoom_max = zoom_levels[zl_cnt - 1] / 100.0; | |
| 35 | 36 | ||
| 36 | 37 | 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; | |
| 39 | 42 | } | |
| 40 | 43 | ||
| 41 | 44 | if (win) { | |
| @@ -102,32 +105,20 @@ void img_check_pan(img_t *img, win_t *win) { | |||
| 102 | 105 | void img_render(img_t *img, win_t *win) { | |
| 103 | 106 | int sx, sy, sw, sh; | |
| 104 | 107 | int dx, dy, dw, dh; | |
| 105 | − | float zw, zh; | |
| 106 | 108 | ||
| 107 | 109 | if (!img || !win || !imlib_context_get_image()) | |
| 108 | 110 | return; | |
| 109 | 111 | ||
| 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) | |
| 122 | 115 | img->zoom = 1.0; | |
| 123 | 116 | } | |
| 124 | 117 | ||
| 125 | 118 | if (!img->re) { | |
| 126 | 119 | /* rendered for the first time */ | |
| 127 | 120 | 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); | |
| 131 | 122 | } | |
| 132 | 123 | ||
| 133 | 124 | if (img->checkpan) { | |
| @@ -167,14 +158,37 @@ void img_render(img_t *img, win_t *win) { | |||
| 167 | 158 | win_draw(win); | |
| 168 | 159 | } | |
| 169 | 160 | ||
| 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 | + | ||
| 170 | 186 | int img_zoom(img_t *img, float z) { | |
| 171 | 187 | if (!img) | |
| 172 | 188 | return 0; | |
| 173 | 189 | ||
| 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); | |
| 178 | 192 | ||
| 179 | 193 | if (z != img->zoom) { | |
| 180 | 194 | img->x -= (img->w * z - img->w * img->zoom) / 2; | |
| @@ -21,11 +21,11 @@ | |||
| 21 | 21 | ||
| 22 | 22 | #include "window.h" | |
| 23 | 23 | ||
| 24 | − | enum scalemode { | |
| 24 | + | typedef enum scalemode_e { | |
| 25 | 25 | SCALE_DOWN = 0, | |
| 26 | 26 | SCALE_FIT, | |
| 27 | 27 | SCALE_ZOOM | |
| 28 | − | }; | |
| 28 | + | } scalemode_t; | |
| 29 | 29 | ||
| 30 | 30 | typedef enum pandir_e { | |
| 31 | 31 | PAN_LEFT = 0, | |
| @@ -52,6 +52,9 @@ void img_free(img_t*); | |||
| 52 | 52 | int img_load(img_t*, const char*); | |
| 53 | 53 | void img_render(img_t*, win_t*); | |
| 54 | 54 | ||
| 55 | + | int img_fit(img_t*, win_t*); | |
| 56 | + | void img_center(img_t*, win_t*); | |
| 57 | + | ||
| 55 | 58 | int img_zoom_in(img_t*); | |
| 56 | 59 | int img_zoom_out(img_t*); | |
| 57 | 60 | ||
| @@ -29,7 +29,7 @@ options_t _options; | |||
| 29 | 29 | const options_t *options = (const options_t*) &_options; | |
| 30 | 30 | ||
| 31 | 31 | 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"); | |
| 33 | 33 | } | |
| 34 | 34 | ||
| 35 | 35 | void print_version() { | |
| @@ -39,19 +39,37 @@ void print_version() { | |||
| 39 | 39 | ||
| 40 | 40 | void parse_options(int argc, char **argv) { | |
| 41 | 41 | unsigned short w, h; | |
| 42 | + | float z; | |
| 42 | 43 | int opt; | |
| 43 | 44 | ||
| 45 | + | _options.scalemode = SCALE_MODE; | |
| 46 | + | _options.zoom = 1.0; | |
| 47 | + | _options.aa = 1; | |
| 48 | + | ||
| 44 | 49 | _options.winw = w = 0; | |
| 45 | 50 | _options.winh = h = 0; | |
| 51 | + | _options.fullscreen = 0; | |
| 46 | 52 | ||
| 47 | − | while ((opt = getopt(argc, argv, "hvw:")) != -1) { | |
| 53 | + | while ((opt = getopt(argc, argv, "dfhpsvw:Zz:")) != -1) { | |
| 48 | 54 | switch (opt) { | |
| 49 | 55 | case '?': | |
| 50 | 56 | print_usage(); | |
| 51 | 57 | exit(1); | |
| 58 | + | case 'd': | |
| 59 | + | _options.scalemode = SCALE_DOWN; | |
| 60 | + | break; | |
| 61 | + | case 'f': | |
| 62 | + | _options.fullscreen = 1; | |
| 63 | + | break; | |
| 52 | 64 | case 'h': | |
| 53 | 65 | print_usage(); | |
| 54 | 66 | exit(0); | |
| 67 | + | case 'p': | |
| 68 | + | _options.aa = 0; | |
| 69 | + | break; | |
| 70 | + | case 's': | |
| 71 | + | _options.scalemode = SCALE_FIT; | |
| 72 | + | break; | |
| 55 | 73 | case 'v': | |
| 56 | 74 | print_version(); | |
| 57 | 75 | exit(0); | |
| @@ -65,6 +83,20 @@ void parse_options(int argc, char **argv) { | |||
| 65 | 83 | _options.winh = (int) h; | |
| 66 | 84 | } | |
| 67 | 85 | 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; | |
| 68 | 100 | } | |
| 69 | 101 | } | |
| 70 | 102 | ||
| @@ -19,11 +19,19 @@ | |||
| 19 | 19 | #ifndef OPTIONS_H | |
| 20 | 20 | #define OPTIONS_H | |
| 21 | 21 | ||
| 22 | + | #include "image.h" | |
| 23 | + | ||
| 22 | 24 | typedef struct options_s { | |
| 23 | 25 | const char **filenames; | |
| 24 | 26 | int filecnt; | |
| 27 | + | ||
| 28 | + | scalemode_t scalemode; | |
| 29 | + | float zoom; | |
| 30 | + | unsigned char aa; | |
| 31 | + | ||
| 25 | 32 | int winw; | |
| 26 | 33 | int winh; | |
| 34 | + | unsigned char fullscreen; | |
| 27 | 35 | } options_t; | |
| 28 | 36 | ||
| 29 | 37 | extern const options_t *options; | |
| @@ -3,11 +3,12 @@ | |||
| 3 | 3 | sxiv \- Simple (or small or suckless) X Image Viewer | |
| 4 | 4 | .SH SYNOPSIS | |
| 5 | 5 | .B sxiv | |
| 6 | − | .RB [ \-hv ] | |
| 7 | − | [ | |
| 8 | − | .B \-w | |
| 6 | + | .RB [ \-dfhpsvZ ] | |
| 7 | + | .RB [ \-w | |
| 9 | 8 | .IB WIDTH x HEIGHT | |
| 10 | 9 | ] | |
| 10 | + | .RB [ \-z | |
| 11 | + | .IR ZOOM ] | |
| 11 | 12 | .IR FILE ... | |
| 12 | 13 | .SH DESCRIPTION | |
| 13 | 14 | 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 | |||
| 17 | 18 | manager. | |
| 18 | 19 | .SH OPTIONS | |
| 19 | 20 | .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 | |
| 20 | 27 | .B \-h | |
| 21 | 28 | Print brief usage information to standard output and exit. | |
| 22 | 29 | .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 | |
| 23 | 36 | .B \-v | |
| 24 | 37 | Print version information to standard output and exit. | |
| 25 | 38 | .TP | |
| @@ -32,6 +45,14 @@ If | |||
| 32 | 45 | .I HEIGHT | |
| 33 | 46 | is omitted, height is also set to | |
| 34 | 47 | .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. | |
| 35 | 56 | .SH KEYBOARD COMMANDS | |
| 36 | 57 | .SS General | |
| 37 | 58 | .TP | |
| @@ -54,6 +54,7 @@ void win_open(win_t *win) { | |||
| 54 | 54 | win->bgcol = bgcol.pixel; | |
| 55 | 55 | win->pm = 0; | |
| 56 | 56 | ||
| 57 | + | win->fullscreen = 0; | |
| 57 | 58 | win->w = MIN(options->winw, e->scrw); | |
| 58 | 59 | win->h = MIN(options->winh, e->scrh); | |
| 59 | 60 | win->x = (e->scrw - win->w) / 2; | |
| @@ -81,6 +82,9 @@ void win_open(win_t *win) { | |||
| 81 | 82 | ||
| 82 | 83 | XMapWindow(e->dpy, win->xwin); | |
| 83 | 84 | XFlush(e->dpy); | |
| 85 | + | ||
| 86 | + | if (options->fullscreen) | |
| 87 | + | win_toggle_fullscreen(win); | |
| 84 | 88 | } | |
| 85 | 89 | ||
| 86 | 90 | void win_close(win_t *win) { | |
| @@ -45,7 +45,7 @@ typedef struct win_s { | |||
| 45 | 45 | int y; | |
| 46 | 46 | ||
| 47 | 47 | int bw; | |
| 48 | − | int fullscreen; | |
| 48 | + | unsigned char fullscreen; | |
| 49 | 49 | } win_t; | |
| 50 | 50 | ||
| 51 | 51 | void win_open(win_t*); | |