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