From 17e2a795bbcdbf9bc8eb07e2aaca0a494871b9e8 Mon Sep 17 00:00:00 2001 From: Bert Date: Thu, 27 Jan 2011 16:15:43 +0100 Subject: Added -w cmdline option --- README.md | 6 ++++-- options.c | 25 +++++++++++++++++++++++-- options.h | 2 ++ sxiv.1 | 14 ++++++++++++++ window.c | 9 +++------ 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 36c335b..c058407 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,10 @@ check and change them, so that they fit your needs. Usage ----- -sxiv has no useful command line options yet, but they will be added in the next -releases. Right now, it simply displays all files given on the command line. +sxiv supports the following command-line options: + + -w WIDTHxHEIGHT set window width to WIDTH and height to HEIGHT + (if HEIGHT is omitted, height is also set to WIDTH) Use the following keys to control sxiv: diff --git a/options.c b/options.c index 50f2012..b0beae9 100644 --- a/options.c +++ b/options.c @@ -29,7 +29,7 @@ options_t _options; const options_t *options = (const options_t*) &_options; void print_usage() { - printf("usage: sxiv [-hv] FILES...\n"); + printf("usage: sxiv [-hv] [-w WIDTH[xHEIGHT]] FILES...\n"); } void print_version() { @@ -38,9 +38,13 @@ void print_version() { } void parse_options(int argc, char **argv) { + unsigned short w, h; int opt; - while ((opt = getopt(argc, argv, "hv")) != -1) { + _options.winw = w = 0; + _options.winh = h = 0; + + while ((opt = getopt(argc, argv, "hvw:")) != -1) { switch (opt) { case '?': print_usage(); @@ -51,9 +55,26 @@ void parse_options(int argc, char **argv) { case 'v': print_version(); exit(0); + case 'w': + if (!sscanf(optarg, "%hux%hu", &w, &h)) { + fprintf(stderr, "sxiv: invalid argument for option -w: %s\n", + optarg); + exit(1); + } else { + _options.winw = (int) w; + _options.winh = (int) h; + } + break; } } + if (!_options.winw) { + _options.winw = WIN_WIDTH; + _options.winh = WIN_HEIGHT; + } else if (!_options.winh) { + _options.winh = _options.winw; + } + _options.filenames = (const char**) argv + optind; _options.filecnt = argc - optind; } diff --git a/options.h b/options.h index d74cb3f..03acf7c 100644 --- a/options.h +++ b/options.h @@ -22,6 +22,8 @@ typedef struct options_s { const char **filenames; int filecnt; + int winw; + int winh; } options_t; extern const options_t *options; diff --git a/sxiv.1 b/sxiv.1 index f7994c7..53f5b99 100644 --- a/sxiv.1 +++ b/sxiv.1 @@ -4,6 +4,10 @@ sxiv \- Simple (or small or suckless) X Image Viewer .SH SYNOPSIS .B sxiv .RB [ \-hv ] +[ +.B \-w +.IB WIDTH x HEIGHT +] .IR FILE ... .SH DESCRIPTION sxiv is a simple image viewer for X. It only has the most basic features @@ -18,6 +22,16 @@ Print brief usage information to standard output and exit. .TP .B \-v Print version information to standard output and exit. +.TP +.BI "\-w " WIDTH x HEIGHT +Set window width to +.I WIDTH +and height to +.IR HEIGHT . +If +.I HEIGHT +is omitted, height is also set to +.IR WIDTH . .SH KEYBOARD COMMANDS .SS General .TP diff --git a/window.c b/window.c index e1b8ee7..89340f7 100644 --- a/window.c +++ b/window.c @@ -23,6 +23,7 @@ #include #include "sxiv.h" +#include "options.h" #include "window.h" GC bgc; @@ -53,12 +54,8 @@ void win_open(win_t *win) { win->bgcol = bgcol.pixel; win->pm = 0; - win->w = WIN_WIDTH; - win->h = WIN_HEIGHT; - if (win->w > e->scrw) - win->w = e->scrw; - if (win->h > e->scrh) - win->h = e->scrh; + win->w = MIN(options->winw, e->scrw); + win->h = MIN(options->winh, e->scrh); win->x = (e->scrw - win->w) / 2; win->y = (e->scrh - win->h) / 2; -- cgit v1.2.3-54-g00ecf