A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

172 lines
4.1 KiB

  1. /* Copyright 2011 Bert Muennich
  2. *
  3. * This file is part of sxiv.
  4. *
  5. * sxiv is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * sxiv is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #define _POSIX_C_SOURCE 200112L
  19. #define _IMAGE_CONFIG
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include "options.h"
  25. #include "util.h"
  26. #include "config.h"
  27. options_t _options;
  28. const options_t *options = (const options_t*) &_options;
  29. void print_usage(void)
  30. {
  31. printf("usage: sxiv [-abcfhioqrtvZ] [-G GAMMA] [-g GEOMETRY] [-n NUM] "
  32. "[-N NAME] [-S DELAY] [-s MODE] [-z ZOOM] FILES...\n");
  33. }
  34. void print_version(void)
  35. {
  36. printf("sxiv %s - Simple X Image Viewer\n", VERSION);
  37. }
  38. void parse_options(int argc, char **argv)
  39. {
  40. int n, opt;
  41. char *end, *s;
  42. const char *scalemodes = "dfwh";
  43. _options.from_stdin = false;
  44. _options.to_stdout = false;
  45. _options.recursive = false;
  46. _options.startnum = 0;
  47. _options.scalemode = SCALE_DOWN;
  48. _options.zoom = 1.0;
  49. _options.animate = false;
  50. _options.gamma = 0;
  51. _options.slideshow = 0;
  52. _options.fullscreen = false;
  53. _options.hide_bar = false;
  54. _options.geometry = NULL;
  55. _options.res_name = NULL;
  56. _options.quiet = false;
  57. _options.thumb_mode = false;
  58. _options.clean_cache = false;
  59. while ((opt = getopt(argc, argv, "abcfG:g:hin:N:oqrS:s:tvZz:")) != -1) {
  60. switch (opt) {
  61. case '?':
  62. print_usage();
  63. exit(EXIT_FAILURE);
  64. case 'a':
  65. _options.animate = true;
  66. break;
  67. case 'b':
  68. _options.hide_bar = true;
  69. break;
  70. case 'c':
  71. _options.clean_cache = true;
  72. break;
  73. case 'f':
  74. _options.fullscreen = true;
  75. break;
  76. case 'G':
  77. n = strtol(optarg, &end, 0);
  78. if (*end != '\0') {
  79. fprintf(stderr, "sxiv: invalid argument for option -G: %s\n", optarg);
  80. exit(EXIT_FAILURE);
  81. }
  82. _options.gamma = n;
  83. break;
  84. case 'g':
  85. _options.geometry = optarg;
  86. break;
  87. case 'h':
  88. print_usage();
  89. exit(EXIT_SUCCESS);
  90. case 'i':
  91. _options.from_stdin = true;
  92. break;
  93. case 'n':
  94. n = strtol(optarg, &end, 0);
  95. if (*end != '\0' || n <= 0) {
  96. fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", optarg);
  97. exit(EXIT_FAILURE);
  98. }
  99. _options.startnum = n - 1;
  100. break;
  101. case 'N':
  102. _options.res_name = optarg;
  103. break;
  104. case 'o':
  105. _options.to_stdout = true;
  106. break;
  107. case 'q':
  108. _options.quiet = true;
  109. break;
  110. case 'r':
  111. _options.recursive = true;
  112. break;
  113. case 'S':
  114. n = strtol(optarg, &end, 0);
  115. if (*end != '\0' || n <= 0) {
  116. fprintf(stderr, "sxiv: invalid argument for option -S: %s\n", optarg);
  117. exit(EXIT_FAILURE);
  118. }
  119. _options.slideshow = n;
  120. break;
  121. case 's':
  122. s = strchr(scalemodes, optarg[0]);
  123. if (s == NULL || *s == '\0' || strlen(optarg) != 1) {
  124. fprintf(stderr, "sxiv: invalid argument for option -s: %s\n", optarg);
  125. exit(EXIT_FAILURE);
  126. }
  127. _options.scalemode = s - scalemodes;
  128. break;
  129. case 't':
  130. _options.thumb_mode = true;
  131. break;
  132. case 'v':
  133. print_version();
  134. exit(EXIT_SUCCESS);
  135. case 'Z':
  136. _options.scalemode = SCALE_ZOOM;
  137. _options.zoom = 1.0;
  138. break;
  139. case 'z':
  140. n = strtol(optarg, &end, 0);
  141. if (*end != '\0' || n <= 0) {
  142. fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", optarg);
  143. exit(EXIT_FAILURE);
  144. }
  145. _options.scalemode = SCALE_ZOOM;
  146. _options.zoom = (float) n / 100.0;
  147. break;
  148. }
  149. }
  150. _options.filenames = argv + optind;
  151. _options.filecnt = argc - optind;
  152. if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
  153. _options.filenames++;
  154. _options.filecnt--;
  155. _options.from_stdin = true;
  156. }
  157. }