1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2019 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <stdio.h>
25bf215546Sopenharmony_ci#include <string.h>
26bf215546Sopenharmony_ci#include <stdlib.h>
27bf215546Sopenharmony_ci#include <string.h>
28bf215546Sopenharmony_ci#include <errno.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "overlay_params.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "util/os_socket.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_cistatic enum overlay_param_position
35bf215546Sopenharmony_ciparse_position(const char *str)
36bf215546Sopenharmony_ci{
37bf215546Sopenharmony_ci   if (!str || !strcmp(str, "top-left"))
38bf215546Sopenharmony_ci      return LAYER_POSITION_TOP_LEFT;
39bf215546Sopenharmony_ci   if (!strcmp(str, "top-right"))
40bf215546Sopenharmony_ci      return LAYER_POSITION_TOP_RIGHT;
41bf215546Sopenharmony_ci   if (!strcmp(str, "bottom-left"))
42bf215546Sopenharmony_ci      return LAYER_POSITION_BOTTOM_LEFT;
43bf215546Sopenharmony_ci   if (!strcmp(str, "bottom-right"))
44bf215546Sopenharmony_ci      return LAYER_POSITION_BOTTOM_RIGHT;
45bf215546Sopenharmony_ci   return LAYER_POSITION_TOP_LEFT;
46bf215546Sopenharmony_ci}
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_cistatic FILE *
49bf215546Sopenharmony_ciparse_output_file(const char *str)
50bf215546Sopenharmony_ci{
51bf215546Sopenharmony_ci   return fopen(str, "w+");
52bf215546Sopenharmony_ci}
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_cistatic int
55bf215546Sopenharmony_ciparse_control(const char *str)
56bf215546Sopenharmony_ci{
57bf215546Sopenharmony_ci   int ret = os_socket_listen_abstract(str, 1);
58bf215546Sopenharmony_ci   if (ret < 0) {
59bf215546Sopenharmony_ci      fprintf(stderr, "ERROR: Couldn't create socket pipe at '%s'\n", str);
60bf215546Sopenharmony_ci      fprintf(stderr, "ERROR: '%s'\n", strerror(errno));
61bf215546Sopenharmony_ci      return ret;
62bf215546Sopenharmony_ci   }
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   os_socket_block(ret, false);
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   return ret;
67bf215546Sopenharmony_ci}
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_cistatic uint32_t
70bf215546Sopenharmony_ciparse_fps_sampling_period(const char *str)
71bf215546Sopenharmony_ci{
72bf215546Sopenharmony_ci   return strtol(str, NULL, 0) * 1000;
73bf215546Sopenharmony_ci}
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_cistatic bool
76bf215546Sopenharmony_ciparse_no_display(const char *str)
77bf215546Sopenharmony_ci{
78bf215546Sopenharmony_ci   return strtol(str, NULL, 0) != 0;
79bf215546Sopenharmony_ci}
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_cistatic unsigned
82bf215546Sopenharmony_ciparse_unsigned(const char *str)
83bf215546Sopenharmony_ci{
84bf215546Sopenharmony_ci   return strtol(str, NULL, 0);
85bf215546Sopenharmony_ci}
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci#define parse_width(s) parse_unsigned(s)
88bf215546Sopenharmony_ci#define parse_height(s) parse_unsigned(s)
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_cistatic bool
91bf215546Sopenharmony_ciparse_help(const char *str)
92bf215546Sopenharmony_ci{
93bf215546Sopenharmony_ci   fprintf(stderr, "Layer params using VK_LAYER_MESA_OVERLAY_CONFIG=\n");
94bf215546Sopenharmony_ci#define OVERLAY_PARAM_BOOL(name)                \
95bf215546Sopenharmony_ci   fprintf(stderr, "\t%s=0|1\n", #name);
96bf215546Sopenharmony_ci#define OVERLAY_PARAM_CUSTOM(name)
97bf215546Sopenharmony_ci   OVERLAY_PARAMS
98bf215546Sopenharmony_ci#undef OVERLAY_PARAM_BOOL
99bf215546Sopenharmony_ci#undef OVERLAY_PARAM_CUSTOM
100bf215546Sopenharmony_ci   fprintf(stderr, "\tposition=top-left|top-right|bottom-left|bottom-right\n");
101bf215546Sopenharmony_ci   fprintf(stderr, "\tfps_sampling_period=number-of-milliseconds\n");
102bf215546Sopenharmony_ci   fprintf(stderr, "\tno_display=0|1\n");
103bf215546Sopenharmony_ci   fprintf(stderr, "\toutput_file=/path/to/output.txt\n");
104bf215546Sopenharmony_ci   fprintf(stderr, "\twidth=width-in-pixels\n");
105bf215546Sopenharmony_ci   fprintf(stderr, "\theight=height-in-pixels\n");
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   return true;
108bf215546Sopenharmony_ci}
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_cistatic bool is_delimiter(char c)
111bf215546Sopenharmony_ci{
112bf215546Sopenharmony_ci   return c == 0 || c == ',' || c == ':' || c == ';' || c == '=';
113bf215546Sopenharmony_ci}
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_cistatic int
116bf215546Sopenharmony_ciparse_string(const char *s, char *out_param, char *out_value)
117bf215546Sopenharmony_ci{
118bf215546Sopenharmony_ci   int i = 0;
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   for (; !is_delimiter(*s); s++, out_param++, i++)
121bf215546Sopenharmony_ci      *out_param = *s;
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   *out_param = 0;
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   if (*s == '=') {
126bf215546Sopenharmony_ci      s++;
127bf215546Sopenharmony_ci      i++;
128bf215546Sopenharmony_ci      for (; !is_delimiter(*s); s++, out_value++, i++)
129bf215546Sopenharmony_ci         *out_value = *s;
130bf215546Sopenharmony_ci   } else
131bf215546Sopenharmony_ci      *(out_value++) = '1';
132bf215546Sopenharmony_ci   *out_value = 0;
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci   if (*s && is_delimiter(*s)) {
135bf215546Sopenharmony_ci      s++;
136bf215546Sopenharmony_ci      i++;
137bf215546Sopenharmony_ci   }
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci   if (*s && !i) {
140bf215546Sopenharmony_ci      fprintf(stderr, "mesa-overlay: syntax error: unexpected '%c' (%i) while "
141bf215546Sopenharmony_ci              "parsing a string\n", *s, *s);
142bf215546Sopenharmony_ci      fflush(stderr);
143bf215546Sopenharmony_ci   }
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   return i;
146bf215546Sopenharmony_ci}
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ciconst char *overlay_param_names[] = {
149bf215546Sopenharmony_ci#define OVERLAY_PARAM_BOOL(name) #name,
150bf215546Sopenharmony_ci#define OVERLAY_PARAM_CUSTOM(name)
151bf215546Sopenharmony_ci   OVERLAY_PARAMS
152bf215546Sopenharmony_ci#undef OVERLAY_PARAM_BOOL
153bf215546Sopenharmony_ci#undef OVERLAY_PARAM_CUSTOM
154bf215546Sopenharmony_ci};
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_civoid
157bf215546Sopenharmony_ciparse_overlay_env(struct overlay_params *params,
158bf215546Sopenharmony_ci                  const char *env)
159bf215546Sopenharmony_ci{
160bf215546Sopenharmony_ci   uint32_t num;
161bf215546Sopenharmony_ci   char key[256], value[256];
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   memset(params, 0, sizeof(*params));
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   /* Visible by default */
166bf215546Sopenharmony_ci   params->enabled[OVERLAY_PARAM_ENABLED_fps] = true;
167bf215546Sopenharmony_ci   params->enabled[OVERLAY_PARAM_ENABLED_frame_timing] = true;
168bf215546Sopenharmony_ci   params->enabled[OVERLAY_PARAM_ENABLED_device] = true;
169bf215546Sopenharmony_ci   params->enabled[OVERLAY_PARAM_ENABLED_format] = true;
170bf215546Sopenharmony_ci   params->fps_sampling_period = 500000; /* 500ms */
171bf215546Sopenharmony_ci   params->width = params->height = 300;
172bf215546Sopenharmony_ci   params->control = -1;
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   if (!env)
175bf215546Sopenharmony_ci      return;
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ci   while ((num = parse_string(env, key, value)) != 0) {
178bf215546Sopenharmony_ci      env += num;
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci#define OVERLAY_PARAM_BOOL(name)                                        \
181bf215546Sopenharmony_ci      if (!strcmp(#name, key)) {                                        \
182bf215546Sopenharmony_ci         params->enabled[OVERLAY_PARAM_ENABLED_##name] =                \
183bf215546Sopenharmony_ci            strtol(value, NULL, 0);                                     \
184bf215546Sopenharmony_ci         continue;                                                      \
185bf215546Sopenharmony_ci      }
186bf215546Sopenharmony_ci#define OVERLAY_PARAM_CUSTOM(name)               \
187bf215546Sopenharmony_ci      if (!strcmp(#name, key)) {                 \
188bf215546Sopenharmony_ci         params->name = parse_##name(value);     \
189bf215546Sopenharmony_ci         continue;                               \
190bf215546Sopenharmony_ci      }
191bf215546Sopenharmony_ci      OVERLAY_PARAMS
192bf215546Sopenharmony_ci#undef OVERLAY_PARAM_BOOL
193bf215546Sopenharmony_ci#undef OVERLAY_PARAM_CUSTOM
194bf215546Sopenharmony_ci      fprintf(stderr, "Unknown option '%s'\n", key);
195bf215546Sopenharmony_ci   }
196bf215546Sopenharmony_ci}
197