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.
 
 
 
 
 
 

183 lines
4.5 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 [-abcfhiopqrtvZ] [-A FRAMERATE] [-e WID] [-G GAMMA] "
  31. "[-g GEOMETRY] [-N NAME] [-n NUM] [-S DELAY] [-s MODE] [-z ZOOM] "
  32. "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. progname = strrchr(argv[0], '/');
  44. progname = progname ? progname + 1 : argv[0];
  45. _options.from_stdin = false;
  46. _options.to_stdout = false;
  47. _options.recursive = false;
  48. _options.startnum = 0;
  49. _options.scalemode = SCALE_DOWN;
  50. _options.zoom = 1.0;
  51. _options.animate = false;
  52. _options.gamma = 0;
  53. _options.slideshow = 0;
  54. _options.framerate = 0;
  55. _options.fullscreen = false;
  56. _options.embed = 0;
  57. _options.hide_bar = false;
  58. _options.geometry = NULL;
  59. _options.res_name = NULL;
  60. _options.quiet = false;
  61. _options.thumb_mode = false;
  62. _options.clean_cache = false;
  63. _options.private_mode = false;
  64. while ((opt = getopt(argc, argv, "A:abce:fG:g:hin:N:opqrS:s:tvZz:")) != -1) {
  65. switch (opt) {
  66. case '?':
  67. print_usage();
  68. exit(EXIT_FAILURE);
  69. case 'A':
  70. n = strtol(optarg, &end, 0);
  71. if (*end != '\0' || n <= 0)
  72. error(EXIT_FAILURE, 0, "Invalid argument for option -A: %s", optarg);
  73. _options.framerate = n;
  74. /* fall through */
  75. case 'a':
  76. _options.animate = true;
  77. break;
  78. case 'b':
  79. _options.hide_bar = true;
  80. break;
  81. case 'c':
  82. _options.clean_cache = true;
  83. break;
  84. case 'e':
  85. n = strtol(optarg, &end, 0);
  86. if (*end != '\0')
  87. error(EXIT_FAILURE, 0, "Invalid argument for option -e: %s", optarg);
  88. _options.embed = n;
  89. break;
  90. case 'f':
  91. _options.fullscreen = true;
  92. break;
  93. case 'G':
  94. n = strtol(optarg, &end, 0);
  95. if (*end != '\0')
  96. error(EXIT_FAILURE, 0, "Invalid argument for option -G: %s", optarg);
  97. _options.gamma = n;
  98. break;
  99. case 'g':
  100. _options.geometry = optarg;
  101. break;
  102. case 'h':
  103. print_usage();
  104. exit(EXIT_SUCCESS);
  105. case 'i':
  106. _options.from_stdin = true;
  107. break;
  108. case 'n':
  109. n = strtol(optarg, &end, 0);
  110. if (*end != '\0' || n <= 0)
  111. error(EXIT_FAILURE, 0, "Invalid argument for option -n: %s", optarg);
  112. _options.startnum = n - 1;
  113. break;
  114. case 'N':
  115. _options.res_name = optarg;
  116. break;
  117. case 'o':
  118. _options.to_stdout = true;
  119. break;
  120. case 'p':
  121. _options.private_mode = true;
  122. break;
  123. case 'q':
  124. _options.quiet = true;
  125. break;
  126. case 'r':
  127. _options.recursive = true;
  128. break;
  129. case 'S':
  130. n = strtof(optarg, &end) * 10;
  131. if (*end != '\0' || n <= 0)
  132. error(EXIT_FAILURE, 0, "Invalid argument for option -S: %s", optarg);
  133. _options.slideshow = n;
  134. break;
  135. case 's':
  136. s = strchr(scalemodes, optarg[0]);
  137. if (s == NULL || *s == '\0' || strlen(optarg) != 1)
  138. error(EXIT_FAILURE, 0, "Invalid argument for option -s: %s", optarg);
  139. _options.scalemode = s - scalemodes;
  140. break;
  141. case 't':
  142. _options.thumb_mode = true;
  143. break;
  144. case 'v':
  145. print_version();
  146. exit(EXIT_SUCCESS);
  147. case 'Z':
  148. _options.scalemode = SCALE_ZOOM;
  149. _options.zoom = 1.0;
  150. break;
  151. case 'z':
  152. n = strtol(optarg, &end, 0);
  153. if (*end != '\0' || n <= 0)
  154. error(EXIT_FAILURE, 0, "Invalid argument for option -z: %s", optarg);
  155. _options.scalemode = SCALE_ZOOM;
  156. _options.zoom = (float) n / 100.0;
  157. break;
  158. }
  159. }
  160. _options.filenames = argv + optind;
  161. _options.filecnt = argc - optind;
  162. if (_options.filecnt == 1 && STREQ(_options.filenames[0], "-")) {
  163. _options.filenames++;
  164. _options.filecnt--;
  165. _options.from_stdin = true;
  166. }
  167. }