~sircmpwn/xrgears

ref: bed4c8b5a430f86f710ca40894f7d72f741b2ec9 xrgears/vitamin-k/system/vikSettings.hpp -rw-r--r-- 7.3 KiB
bed4c8b5 — Lubosz Sarnecki settings: add option to disable text overlay. 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * vitamin-k
 *
 * Copyright 2017-2018 Collabora Ltd.
 *
 * Authors: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
 * SPDX-License-Identifier: MIT
 */

#pragma once

#include <stdint.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>

#include <string>
#include <algorithm>

#include "vikLog.hpp"
#include "../render/vikTools.hpp"

namespace vik {
class Settings {
 public:
  enum window_type {
    AUTO = 0,
    KMS,
    XCB,
    WAYLAND_XDG,
    WAYLAND_SHELL,
    KHR_DISPLAY,
    INVALID
  };

  uint32_t gpu = 0;
  uint32_t hmd = 0;

  uint32_t display = 0;
  uint32_t mode = 0;

  VkFormat color_format = VK_FORMAT_B8G8R8A8_UNORM;
  VkPresentModeKHR present_mode = VK_PRESENT_MODE_FIFO_KHR;

  enum window_type type = AUTO;

  bool validation = false;
  bool fullscreen = false;

  bool list_gpus_and_exit = false;
  bool list_screens_and_exit = false;
  bool list_hmds_and_exit = false;
  bool list_formats_and_exit = false;
  bool list_present_modes_and_exit = false;

  bool enable_text_overlay = true;

  std::pair<uint32_t,uint32_t> size = {1280, 720};

  std::string help_string() {
    std::string help =
        "A XR demo for Vulkan and OpenHMD\n"
        "\n"
        "Options:\n"
        "  -s, --size WxH           Size of the output window (default: 1280x720)\n"
        "  -f, --fullscreen         Run fullscreen. Optinally specify display and mode.\n"
        "  -d, --display D          Display to fullscreen on. (default: 0)\n"
        "  -m, --mode M             Mode for fullscreen (wayland-shell only) (default: 0)\n"
        "  -w, --window WS          Window system to use (default: auto)\n"
        "                           [xcb, wayland, wayland-shell, kms]\n"
        "  -g, --gpu GPU            GPU to use (default: 0)\n"
        "      --hmd HMD            HMD to use (default: 0)\n"
        "      --format F           Color format to use (default: VK_FORMAT_B8G8R8A8_UNORM)\n"
        "      --presentmode M      Present mode to use (default: VK_PRESENT_MODE_FIFO_KHR)\n"
        "\n"
        "      --list-gpus          List available GPUs\n"
        "      --list-displays      List available displays\n"
        "      --list-hmds          List available HMDs\n"
        "      --list-formats       List available color formats\n"
        "      --list-presentmodes  List available present modes\n"
        "\n"
        "      --disable-overlay    Disable text overlay\n"
        "  -h, --help               Show this help\n";

 // VK_PRESENT_MODE_FIFO_KHR for vsync
    return help;
  }

  std::pair<uint32_t,uint32_t> parse_size(std::string const& str) {
      std::pair<uint32_t,uint32_t> size;
      auto const dimensions = tools::split(str, 'x');

      if (dimensions.size() != 2
          || !is_number(dimensions[0])
          || !is_number(dimensions[1]))
        vik_log_f("Size must be 2 integers separated with x.");

      size.first = tools::from_string<uint32_t>(dimensions[0]);

      /*
       * Parse the second element (height). If there is none, use the value
       * of the first element for the second (width = height)
       */
      if (dimensions.size() > 1)
          size.second = tools::from_string<uint32_t>(dimensions[1]);
      else
          size.second = size.first;

      return size;
  }

  bool parse_args(int argc, char *argv[]) {
    /* Setting '+' in the optstring is the same as setting POSIXLY_CORRECT in
     * the enviroment. It tells getopt to stop parsing argv when it encounters
     * the first non-option argument; it also prevents getopt from permuting
     * argv during parsing.
     *
     * The initial ':' in the optstring makes getopt return ':' when an option
     * is missing a required argument.
     */

    int option_index = -1;
    static const char *optstring = "hs:w:vfg:d:m:";


    struct option long_options[] = {
      {"window", 1, 0, 0},
      {"size", 1, 0, 0},
      {"help", 0, 0, 0},
      {"validation", 0, 0, 0},
      {"fullscreen", 0, 0, 0},
      {"display", 1, 0, 0},
      {"mode", 1, 0, 0},
      {"gpu", 1, 0, 0},
      {"hmd", 1, 0, 0},
      {"format", 1, 0, 0},
      {"presentmode", 1, 0, 0},
      {"list-gpus", 0, 0, 0},
      {"list-displays", 0, 0, 0},
      {"list-hmds", 0, 0, 0},
      {"list-formats", 0, 0, 0},
      {"list-presentmodes", 0, 0, 0},
      {"disable-overlay", 0, 0, 0},
      {0, 0, 0, 0}
    };

    std::string optname;

    int opt;
    while ((opt = getopt_long(argc, argv, optstring,
                              long_options, &option_index)) != -1) {
      if (opt == '?' || opt == ':')
        return false;

      if (option_index != -1)
        optname = long_options[option_index].name;

      if (opt == 'h' || optname == "help") {
        printf("%s\n", help_string().c_str());
        exit(0);
      } else if (opt == 'v' || optname == "validation") {
        validation = true;
      } else if (optname == "list-gpus") {
        list_gpus_and_exit = true;
      } else if (optname == "list-displays") {
        list_screens_and_exit = true;
      } else if (optname == "list-hmds") {
        list_hmds_and_exit = true;
      } else if (optname == "list-formats") {
        list_formats_and_exit = true;
      } else if (optname == "list-presentmodes") {
        list_present_modes_and_exit = true;
      } else if (optname == "disable-overlay") {
        enable_text_overlay = false;
      } else if (opt == 's' || optname == "size") {
        size = parse_size(optarg);
      } else if (optname == "presentmode") {
        present_mode = Log::string_to_present_mode(optarg);
      } else if (optname == "format") {
        color_format = Log::string_to_color_format(optarg);
      } else if (opt == 'f' || optname == "fullscreen") {
        fullscreen = true;
      } else if (opt == 'd' || optname == "display") {
        display = parse_id(optarg);
        fullscreen = true;
      } else if (opt == 'm' || optname == "mode") {
        mode = parse_id(optarg);
        fullscreen = true;
      } else if (optname == "hmd") {
        hmd = parse_id(optarg);
      } else if (opt == 'g' || optname == "gpu") {
        gpu = parse_id(optarg);
      } else if (opt == 'w' || optname == "window") {
        type = window_type_from_string(optarg);
        if (type == INVALID)
          vik_log_f("option -w given bad display mode");
      } else {
        vik_log_f("Unknown option %s", optname.c_str());
      }
    }

    if (optind != argc)
      vik_log_w("trailing args");

    return true;
  }

  bool is_number(const std::string& str) {
    auto is_not_digit = [](char c) { return !std::isdigit(c); };
    return !str.empty() &&
        std::find_if(str.begin(), str.end(), is_not_digit) == str.end();
  }

  int parse_id(std::string const& str) {
    if (!is_number(str)) {
      vik_log_e("%s is not a valid number", str.c_str());
      return 0;
    }
    return std::stoi(str, nullptr);
  }

  static inline bool streq(const char *a, const char *b) {
    return strcmp(a, b) == 0;
  }

  static window_type window_type_from_string(const char *s) {
    if (streq(s, "auto"))
      return AUTO;
    else if (streq(s, "kms"))
      return KMS;
    else if (streq(s, "xcb"))
      return XCB;
    else if (streq(s, "wayland") || streq(s, "wayland-xdg"))
      return WAYLAND_XDG;
    else if (streq(s, "wayland-shell"))
      return WAYLAND_SHELL;
    else if (streq(s, "khr-display"))
      return KHR_DISPLAY;
    else
      return INVALID;
  }
};
}  // namespace vik