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.
 
 
 
 
 
 

171 lines
4.0 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. #include <stdlib.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include "options.h"
  23. #include "util.h"
  24. #define _IMAGE_CONFIG
  25. #include "config.h"
  26. options_t _options;
  27. const options_t *options = (const options_t*) &_options;
  28. void print_usage(void)
  29. {
  30. printf("usage: sxiv [-abcfhioqrtvZ] [-G GAMMA] [-g GEOMETRY] [-n NUM] "
  31. "[-N NAME] [-S DELAY] [-s MODE] [-z ZOOM] FILES...\n");
  32. }
  33. void print_version(void)
  34. {
  35. printf("sxiv %s - Simple X Image Viewer\n", VERSION);
  36. }
  37. void parse_options(int argc, char **argv)
  38. {
  39. int n, opt;
  40. char *end, *s;
  41. const char *scalemodes = "dfwh";
  42. _options.from_stdin = false;
  43. _options.to_stdout = false;
  44. _options.recursive = false;
  45. _options.startnum = 0;
  46. _options.scalemode = SCALE_DOWN;
  47. _options.zoom = 1.0;
  48. _options.animate = false;
  49. _options.gamma = 0;
  50. _options.slideshow = 0;
  51. _options.fullscreen = false;
  52. _options.hide_bar = false;
  53. _options.geometry = NULL;
  54. _options.res_name = NULL;
  55. _options.quiet = false;
  56. _options.thumb_mode = false;
  57. _options.clean_cache = false;
  58. while ((opt = getopt(argc, argv, "abcfG:g:hin:N:oqrS:s:tvZz:")) != -1) {
  59. switch (opt) {
  60. case '?':
  61. print_usage();
  62. exit(EXIT_FAILURE);
  63. case 'a':
  64. _options.animate = true;
  65. break;
  66. case 'b':
  67. _options.hide_bar = true;
  68. break;
  69. case 'c':
  70. _options.clean_cache = true;
  71. break;
  72. case 'f':
  73. _options.fullscreen = true;
  74. break;
  75. case 'G':
  76. n = strtol(optarg, &end, 0);
  77. if (*end != '\0') {
  78. fprintf(stderr, "sxiv: invalid argument for option -G: %s\n", optarg);
  79. exit(EXIT_FAILURE);
  80. }
  81. _options.gamma = n;
  82. break;
  83. case 'g':
  84. _options.geometry = optarg;
  85. break;
  86. case 'h':
  87. print_usage();
  88. exit(EXIT_SUCCESS);
  89. case 'i':
  90. _options.from_stdin = true;
  91. break;
  92. case 'n':
  93. n = strtol(optarg, &end, 0);
  94. if (*end != '\0' || n <= 0) {
  95. fprintf(stderr, "sxiv: invalid argument for option -n: %s\n", optarg);
  96. exit(EXIT_FAILURE);
  97. }
  98. _options.startnum = n - 1;
  99. break;
  100. case 'N':
  101. _options.res_name = optarg;
  102. break;
  103. case 'o':
  104. _options.to_stdout = true;
  105. break;
  106. case 'q':
  107. _options.quiet = true;
  108. break;
  109. case 'r':
  110. _options.recursive = true;
  111. break;
  112. case 'S':
  113. n = strtol(optarg, &end, 0);
  114. if (*end != '\0' || n <= 0) {
  115. fprintf(stderr, "sxiv: invalid argument for option -S: %s\n", optarg);
  116. exit(EXIT_FAILURE);
  117. }
  118. _options.slideshow = n;
  119. break;
  120. case 's':
  121. s = strchr(scalemodes, optarg[0]);
  122. if (s == NULL || *s == '\0' || strlen(optarg) != 1) {
  123. fprintf(stderr, "sxiv: invalid argument for option -s: %s\n", optarg);
  124. exit(EXIT_FAILURE);
  125. }
  126. _options.scalemode = s - scalemodes;
  127. break;
  128. case 't':
  129. _options.thumb_mode = true;
  130. break;
  131. case 'v':
  132. print_version();
  133. exit(EXIT_SUCCESS);
  134. case 'Z':
  135. _options.scalemode = SCALE_ZOOM;
  136. _options.zoom = 1.0;
  137. break;
  138. case 'z':
  139. n = strtol(optarg, &end, 0);
  140. if (*end != '\0' || n <= 0) {
  141. fprintf(stderr, "sxiv: invalid argument for option -z: %s\n", optarg);
  142. exit(EXIT_FAILURE);
  143. }
  144. _options.scalemode = SCALE_ZOOM;
  145. _options.zoom = (float) n / 100.0;
  146. break;
  147. }
  148. }
  149. _options.filenames = argv + optind;
  150. _options.filecnt = argc - optind;
  151. if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
  152. _options.filenames++;
  153. _options.filecnt--;
  154. _options.from_stdin = true;
  155. }
  156. }