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 #include <string.h>
28 #include <errno.h>
29
30 #include "overlay_params.h"
31
32 #include "util/os_socket.h"
33
34 static enum overlay_param_position
parse_position(const char * str)35 parse_position(const char *str)
36 {
37 if (!str || !strcmp(str, "top-left"))
38 return LAYER_POSITION_TOP_LEFT;
39 if (!strcmp(str, "top-right"))
40 return LAYER_POSITION_TOP_RIGHT;
41 if (!strcmp(str, "bottom-left"))
42 return LAYER_POSITION_BOTTOM_LEFT;
43 if (!strcmp(str, "bottom-right"))
44 return LAYER_POSITION_BOTTOM_RIGHT;
45 return LAYER_POSITION_TOP_LEFT;
46 }
47
48 static const char *
parse_output_file(const char * str)49 parse_output_file(const char *str)
50 {
51 return strdup(str);
52 }
53
54 static const char *
parse_control(const char * str)55 parse_control(const char *str)
56 {
57 return strdup(str);
58 }
59
60 static uint32_t
parse_fps_sampling_period(const char * str)61 parse_fps_sampling_period(const char *str)
62 {
63 return strtol(str, NULL, 0) * 1000;
64 }
65
66 static bool
parse_no_display(const char * str)67 parse_no_display(const char *str)
68 {
69 return strtol(str, NULL, 0) != 0;
70 }
71
72 static unsigned
parse_unsigned(const char * str)73 parse_unsigned(const char *str)
74 {
75 return strtol(str, NULL, 0);
76 }
77
78 #define parse_width(s) parse_unsigned(s)
79 #define parse_height(s) parse_unsigned(s)
80
81 static bool
parse_help(const char * str)82 parse_help(const char *str)
83 {
84 fprintf(stderr, "Layer params using VK_LAYER_MESA_OVERLAY_CONFIG=\n");
85 #define OVERLAY_PARAM_BOOL(name) \
86 fprintf(stderr, "\t%s=0|1\n", #name);
87 #define OVERLAY_PARAM_CUSTOM(name)
88 OVERLAY_PARAMS
89 #undef OVERLAY_PARAM_BOOL
90 #undef OVERLAY_PARAM_CUSTOM
91 fprintf(stderr, "\tposition=top-left|top-right|bottom-left|bottom-right\n");
92 fprintf(stderr, "\tfps_sampling_period=number-of-milliseconds\n");
93 fprintf(stderr, "\tno_display=0|1\n");
94 fprintf(stderr, "\toutput_file=/path/to/output.txt\n");
95 fprintf(stderr, "\twidth=width-in-pixels\n");
96 fprintf(stderr, "\theight=height-in-pixels\n");
97
98 return true;
99 }
100
is_delimiter(char c)101 static bool is_delimiter(char c)
102 {
103 return c == 0 || c == ',' || c == ':' || c == ';' || c == '=';
104 }
105
106 static int
parse_string(const char * s,char * out_param,char * out_value)107 parse_string(const char *s, char *out_param, char *out_value)
108 {
109 int i = 0;
110
111 for (; !is_delimiter(*s); s++, out_param++, i++)
112 *out_param = *s;
113
114 *out_param = 0;
115
116 if (*s == '=') {
117 s++;
118 i++;
119 for (; !is_delimiter(*s); s++, out_value++, i++)
120 *out_value = *s;
121 } else
122 *(out_value++) = '1';
123 *out_value = 0;
124
125 if (*s && is_delimiter(*s)) {
126 s++;
127 i++;
128 }
129
130 if (*s && !i) {
131 fprintf(stderr, "mesa-overlay: syntax error: unexpected '%c' (%i) while "
132 "parsing a string\n", *s, *s);
133 fflush(stderr);
134 }
135
136 return i;
137 }
138
139 const char *overlay_param_names[] = {
140 #define OVERLAY_PARAM_BOOL(name) #name,
141 #define OVERLAY_PARAM_CUSTOM(name)
142 OVERLAY_PARAMS
143 #undef OVERLAY_PARAM_BOOL
144 #undef OVERLAY_PARAM_CUSTOM
145 };
146
147 void
parse_overlay_env(struct overlay_params * params,const char * env)148 parse_overlay_env(struct overlay_params *params,
149 const char *env)
150 {
151 uint32_t num;
152 char key[256], value[256];
153
154 memset(params, 0, sizeof(*params));
155
156 /* Visible by default */
157 params->enabled[OVERLAY_PARAM_ENABLED_fps] = true;
158 params->enabled[OVERLAY_PARAM_ENABLED_frame_timing] = true;
159 params->enabled[OVERLAY_PARAM_ENABLED_device] = true;
160 params->enabled[OVERLAY_PARAM_ENABLED_format] = true;
161 params->fps_sampling_period = 500000; /* 500ms */
162 params->width = params->height = 300;
163 params->control = NULL;
164
165 if (!env)
166 return;
167
168 while ((num = parse_string(env, key, value)) != 0) {
169 env += num;
170
171 #define OVERLAY_PARAM_BOOL(name) \
172 if (!strcmp(#name, key)) { \
173 params->enabled[OVERLAY_PARAM_ENABLED_##name] = \
174 strtol(value, NULL, 0); \
175 continue; \
176 }
177 #define OVERLAY_PARAM_CUSTOM(name) \
178 if (!strcmp(#name, key)) { \
179 params->name = parse_##name(value); \
180 continue; \
181 }
182 OVERLAY_PARAMS
183 #undef OVERLAY_PARAM_BOOL
184 #undef OVERLAY_PARAM_CUSTOM
185 fprintf(stderr, "Unknown option '%s'\n", key);
186 }
187 }
188