| 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 | |
| 28 | options_t _options; |
| 29 | const options_t *options = (const options_t*) &_options; |
| 30 | |
| 31 | void print_usage() { |
| 32 | printf("usage: sxiv [-dfhpsvWZ] [-w WIDTH[xHEIGHT]] [-z ZOOM] FILES...\n"); |
| 33 | } |
| 34 | |
| 35 | void print_version() { |
| 36 | printf("sxiv - simple x image viewer\n"); |
| 37 | printf("Version %s, written by Bert Muennich\n", VERSION); |
| 38 | } |
| 39 | |
| 40 | void 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 | _options.warn = 0; |
| 54 | |
| 55 | while ((opt = getopt(argc, argv, "dfhpsvWw:Zz:")) != -1) { |
| 56 | switch (opt) { |
| 57 | case '?': |
| 58 | print_usage(); |
| 59 | exit(1); |
| 60 | case 'd': |
| 61 | _options.scalemode = SCALE_DOWN; |
| 62 | break; |
| 63 | case 'f': |
| 64 | _options.fullscreen = 1; |
| 65 | break; |
| 66 | case 'h': |
| 67 | print_usage(); |
| 68 | exit(0); |
| 69 | case 'p': |
| 70 | _options.aa = 0; |
| 71 | break; |
| 72 | case 's': |
| 73 | _options.scalemode = SCALE_FIT; |
| 74 | break; |
| 75 | case 'v': |
| 76 | print_version(); |
| 77 | exit(0); |
| 78 | case 'W': |
| 79 | _options.warn = 1; |
| 80 | break; |
| 81 | case 'w': |
| 82 | if (!sscanf(optarg, "%hux%hu", &w, &h)) { |
| 83 | fprintf(stderr, "sxiv: invalid argument for option -w: %s\n", |
| 84 | optarg); |
| 85 | exit(1); |
| 86 | } else { |
| 87 | _options.winw = (int) w; |
| 88 | _options.winh = (int) h; |
| 89 | } |
| 90 | break; |
| 91 | case 'Z': |
| 92 | _options.scalemode = SCALE_ZOOM; |
| 93 | _options.zoom = 1.0; |
| 94 | break; |
| 95 | case 'z': |
| 96 | _options.scalemode = SCALE_ZOOM; |
| 97 | if (!sscanf(optarg, "%f", &z) || z < 0) { |
| 98 | fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", |
| 99 | optarg); |
| 100 | exit(1); |
| 101 | } else { |
| 102 | _options.zoom = z / 100.0; |
| 103 | } |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (!_options.winw) { |
| 109 | _options.winw = WIN_WIDTH; |
| 110 | _options.winh = WIN_HEIGHT; |
| 111 | } else if (!_options.winh) { |
| 112 | _options.winh = _options.winw; |
| 113 | } |
| 114 | |
| 115 | _options.filenames = (const char**) argv + optind; |
| 116 | _options.filecnt = argc - optind; |
| 117 | } |