Krolyxon
Commit 56cd150

Branches

Refs →

nsxivoptions.c

112 lines · 2.7 KB · C

1/* sxiv: options.c
2 * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19#define _XOPEN_SOURCE
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <unistd.h>
24
25#include "sxiv.h"
26#include "options.h"
27
28options_t _options;
29const options_t *options = (const options_t*) &_options;
30
31void print_usage() {
32 printf("usage: sxiv [-dfhpsvZ] [-w WIDTH[xHEIGHT]] [-z ZOOM] FILES...\n");
33}
34
35void print_version() {
36 printf("sxiv - simple x image viewer\n");
37 printf("Version %s, written by Bert Muennich\n", VERSION);
38}
39
40void parse_options(int argc, char **argv) {
41 unsigned short w, h;
42 float z;
43 int opt;
44
45 _options.scalemode = SCALE_MODE;
46 _options.zoom = 1.0;
47 _options.aa = 1;
48
49 _options.winw = w = 0;
50 _options.winh = h = 0;
51 _options.fullscreen = 0;
52
53 while ((opt = getopt(argc, argv, "dfhpsvw:Zz:")) != -1) {
54 switch (opt) {
55 case '?':
56 print_usage();
57 exit(1);
58 case 'd':
59 _options.scalemode = SCALE_DOWN;
60 break;
61 case 'f':
62 _options.fullscreen = 1;
63 break;
64 case 'h':
65 print_usage();
66 exit(0);
67 case 'p':
68 _options.aa = 0;
69 break;
70 case 's':
71 _options.scalemode = SCALE_FIT;
72 break;
73 case 'v':
74 print_version();
75 exit(0);
76 case 'w':
77 if (!sscanf(optarg, "%hux%hu", &w, &h)) {
78 fprintf(stderr, "sxiv: invalid argument for option -w: %s\n",
79 optarg);
80 exit(1);
81 } else {
82 _options.winw = (int) w;
83 _options.winh = (int) h;
84 }
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;
100 }
101 }
102
103 if (!_options.winw) {
104 _options.winw = WIN_WIDTH;
105 _options.winh = WIN_HEIGHT;
106 } else if (!_options.winh) {
107 _options.winh = _options.winw;
108 }
109
110 _options.filenames = (const char**) argv + optind;
111 _options.filecnt = argc - optind;
112}