d28799ccf06d3dd9e04a0fc5e6c737a2241f8172
[mesa.git] / src / vulkan / overlay-layer / overlay_params.c
1 /*
2 * Copyright © 2019 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #include "overlay_params.h"
29
30 static enum overlay_param_position
31 parse_position(const char *str)
32 {
33 if (!str || !strcmp(str, "top-left"))
34 return LAYER_POSITION_TOP_LEFT;
35 if (!strcmp(str, "top-right"))
36 return LAYER_POSITION_TOP_RIGHT;
37 if (!strcmp(str, "bottom-left"))
38 return LAYER_POSITION_BOTTOM_LEFT;
39 if (!strcmp(str, "bottom-right"))
40 return LAYER_POSITION_BOTTOM_RIGHT;
41 return LAYER_POSITION_TOP_LEFT;
42 }
43
44 static bool
45 parse_help(const char *str)
46 {
47 fprintf(stderr, "Layer params using VK_LAYER_MESA_OVERLAY_CONFIG=\n");
48 #define OVERLAY_PARAM_BOOL(name) \
49 fprintf(stderr, "\t%s=0/1\n", #name);
50 #define OVERLAY_PARAM_CUSTOM(name)
51 OVERLAY_PARAMS
52 #undef OVERLAY_PARAM_BOOL
53 #undef OVERLAY_PARAM_CUSTOM
54 fprintf(stderr, "\tposition=\n"
55 "\t\ttop-left\n"
56 "\t\ttop-right\n"
57 "\t\tbottom-left\n"
58 "\t\tbottom-right\n");
59
60 return true;
61 }
62
63 static bool is_delimiter(char c)
64 {
65 return c == 0 || c == ',' || c == ':' || c == ';' || c == '=';
66 }
67
68 static int
69 parse_string(const char *s, char *out_param, char *out_value)
70 {
71 int i = 0;
72
73 for (; !is_delimiter(*s); s++, out_param++, i++)
74 *out_param = *s;
75
76 *out_param = 0;
77
78 if (*s == '=') {
79 s++;
80 i++;
81 for (; !is_delimiter(*s); s++, out_value++, i++)
82 *out_value = *s;
83 }
84 *out_value = 0;
85
86 if (*s && is_delimiter(*s)) {
87 s++;
88 i++;
89 }
90
91 if (*s && !i) {
92 fprintf(stderr, "mesa-overlay: syntax error: unexpected '%c' (%i) while "
93 "parsing a string\n", *s, *s);
94 fflush(stderr);
95 }
96
97 return i;
98 }
99
100 const char *overlay_param_names[] = {
101 #define OVERLAY_PARAM_BOOL(name) #name,
102 #define OVERLAY_PARAM_CUSTOM(name)
103 OVERLAY_PARAMS
104 #undef OVERLAY_PARAM_BOOL
105 #undef OVERLAY_PARAM_CUSTOM
106 };
107
108 void
109 parse_overlay_env(struct overlay_params *params,
110 const char *env)
111 {
112 uint32_t num;
113 char key[256], value[256];
114
115 memset(params, 0, sizeof(*params));
116
117 /* Visible by default */
118 params->enabled[OVERLAY_PARAM_ENABLED_fps] = true;
119 params->enabled[OVERLAY_PARAM_ENABLED_frame_timing] = true;
120
121 if (!env)
122 return;
123
124 while ((num = parse_string(env, key, value)) != 0) {
125 env += num;
126
127 #define OVERLAY_PARAM_BOOL(name) \
128 if (!strcmp(#name, key)) { \
129 params->enabled[OVERLAY_PARAM_ENABLED_##name] = strtol(value, NULL, 0); \
130 continue; \
131 }
132 #define OVERLAY_PARAM_CUSTOM(name) \
133 if (!strcmp(#name, key)) { \
134 params->name = parse_##name(value); \
135 continue; \
136 }
137 OVERLAY_PARAMS
138 #undef OVERLAY_PARAM_BOOL
139 #undef OVERLAY_PARAM_CUSTOM
140 fprintf(stderr, "Unknown option '%s'\n", key);
141 }
142 }