18a1362adSmaya/*
28a1362adSmaya * Copyright © 2019 Intel Corporation
38a1362adSmaya *
48a1362adSmaya * Permission is hereby granted, free of charge, to any person obtaining a
58a1362adSmaya * copy of this software and associated documentation files (the "Software"),
68a1362adSmaya * to deal in the Software without restriction, including without limitation
78a1362adSmaya * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88a1362adSmaya * and/or sell copies of the Software, and to permit persons to whom the
98a1362adSmaya * Software is furnished to do so, subject to the following conditions:
108a1362adSmaya *
118a1362adSmaya * The above copyright notice and this permission notice (including the next
128a1362adSmaya * paragraph) shall be included in all copies or substantial portions of the
138a1362adSmaya * Software.
148a1362adSmaya *
158a1362adSmaya * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168a1362adSmaya * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178a1362adSmaya * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188a1362adSmaya * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198a1362adSmaya * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
208a1362adSmaya * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
218a1362adSmaya * IN THE SOFTWARE.
228a1362adSmaya */
238a1362adSmaya
248a1362adSmaya#include <stdio.h>
258a1362adSmaya#include <string.h>
268a1362adSmaya#include <stdlib.h>
277ec681f3Smrg#include <string.h>
287ec681f3Smrg#include <errno.h>
298a1362adSmaya
308a1362adSmaya#include "overlay_params.h"
318a1362adSmaya
327ec681f3Smrg#include "util/os_socket.h"
337ec681f3Smrg
348a1362adSmayastatic enum overlay_param_position
358a1362adSmayaparse_position(const char *str)
368a1362adSmaya{
378a1362adSmaya   if (!str || !strcmp(str, "top-left"))
388a1362adSmaya      return LAYER_POSITION_TOP_LEFT;
398a1362adSmaya   if (!strcmp(str, "top-right"))
408a1362adSmaya      return LAYER_POSITION_TOP_RIGHT;
418a1362adSmaya   if (!strcmp(str, "bottom-left"))
428a1362adSmaya      return LAYER_POSITION_BOTTOM_LEFT;
438a1362adSmaya   if (!strcmp(str, "bottom-right"))
448a1362adSmaya      return LAYER_POSITION_BOTTOM_RIGHT;
458a1362adSmaya   return LAYER_POSITION_TOP_LEFT;
468a1362adSmaya}
478a1362adSmaya
488a1362adSmayastatic FILE *
498a1362adSmayaparse_output_file(const char *str)
508a1362adSmaya{
518a1362adSmaya   return fopen(str, "w+");
528a1362adSmaya}
538a1362adSmaya
547ec681f3Smrgstatic int
557ec681f3Smrgparse_control(const char *str)
567ec681f3Smrg{
577ec681f3Smrg   int ret = os_socket_listen_abstract(str, 1);
587ec681f3Smrg   if (ret < 0) {
597ec681f3Smrg      fprintf(stderr, "ERROR: Couldn't create socket pipe at '%s'\n", str);
607ec681f3Smrg      fprintf(stderr, "ERROR: '%s'\n", strerror(errno));
617ec681f3Smrg      return ret;
627ec681f3Smrg   }
637ec681f3Smrg
647ec681f3Smrg   os_socket_block(ret, false);
657ec681f3Smrg
667ec681f3Smrg   return ret;
677ec681f3Smrg}
687ec681f3Smrg
698a1362adSmayastatic uint32_t
708a1362adSmayaparse_fps_sampling_period(const char *str)
718a1362adSmaya{
728a1362adSmaya   return strtol(str, NULL, 0) * 1000;
738a1362adSmaya}
748a1362adSmaya
758a1362adSmayastatic bool
768a1362adSmayaparse_no_display(const char *str)
778a1362adSmaya{
788a1362adSmaya   return strtol(str, NULL, 0) != 0;
798a1362adSmaya}
808a1362adSmaya
818a1362adSmayastatic unsigned
828a1362adSmayaparse_unsigned(const char *str)
838a1362adSmaya{
848a1362adSmaya   return strtol(str, NULL, 0);
858a1362adSmaya}
868a1362adSmaya
878a1362adSmaya#define parse_width(s) parse_unsigned(s)
888a1362adSmaya#define parse_height(s) parse_unsigned(s)
898a1362adSmaya
908a1362adSmayastatic bool
918a1362adSmayaparse_help(const char *str)
928a1362adSmaya{
938a1362adSmaya   fprintf(stderr, "Layer params using VK_LAYER_MESA_OVERLAY_CONFIG=\n");
948a1362adSmaya#define OVERLAY_PARAM_BOOL(name)                \
958a1362adSmaya   fprintf(stderr, "\t%s=0|1\n", #name);
968a1362adSmaya#define OVERLAY_PARAM_CUSTOM(name)
978a1362adSmaya   OVERLAY_PARAMS
988a1362adSmaya#undef OVERLAY_PARAM_BOOL
998a1362adSmaya#undef OVERLAY_PARAM_CUSTOM
1008a1362adSmaya   fprintf(stderr, "\tposition=top-left|top-right|bottom-left|bottom-right\n");
1018a1362adSmaya   fprintf(stderr, "\tfps_sampling_period=number-of-milliseconds\n");
1028a1362adSmaya   fprintf(stderr, "\tno_display=0|1\n");
1038a1362adSmaya   fprintf(stderr, "\toutput_file=/path/to/output.txt\n");
1048a1362adSmaya   fprintf(stderr, "\twidth=width-in-pixels\n");
1058a1362adSmaya   fprintf(stderr, "\theight=height-in-pixels\n");
1068a1362adSmaya
1078a1362adSmaya   return true;
1088a1362adSmaya}
1098a1362adSmaya
1108a1362adSmayastatic bool is_delimiter(char c)
1118a1362adSmaya{
1128a1362adSmaya   return c == 0 || c == ',' || c == ':' || c == ';' || c == '=';
1138a1362adSmaya}
1148a1362adSmaya
1158a1362adSmayastatic int
1168a1362adSmayaparse_string(const char *s, char *out_param, char *out_value)
1178a1362adSmaya{
1188a1362adSmaya   int i = 0;
1198a1362adSmaya
1208a1362adSmaya   for (; !is_delimiter(*s); s++, out_param++, i++)
1218a1362adSmaya      *out_param = *s;
1228a1362adSmaya
1238a1362adSmaya   *out_param = 0;
1248a1362adSmaya
1258a1362adSmaya   if (*s == '=') {
1268a1362adSmaya      s++;
1278a1362adSmaya      i++;
1288a1362adSmaya      for (; !is_delimiter(*s); s++, out_value++, i++)
1298a1362adSmaya         *out_value = *s;
1308a1362adSmaya   } else
1318a1362adSmaya      *(out_value++) = '1';
1328a1362adSmaya   *out_value = 0;
1338a1362adSmaya
1348a1362adSmaya   if (*s && is_delimiter(*s)) {
1358a1362adSmaya      s++;
1368a1362adSmaya      i++;
1378a1362adSmaya   }
1388a1362adSmaya
1398a1362adSmaya   if (*s && !i) {
1408a1362adSmaya      fprintf(stderr, "mesa-overlay: syntax error: unexpected '%c' (%i) while "
1418a1362adSmaya              "parsing a string\n", *s, *s);
1428a1362adSmaya      fflush(stderr);
1438a1362adSmaya   }
1448a1362adSmaya
1458a1362adSmaya   return i;
1468a1362adSmaya}
1478a1362adSmaya
1488a1362adSmayaconst char *overlay_param_names[] = {
1498a1362adSmaya#define OVERLAY_PARAM_BOOL(name) #name,
1508a1362adSmaya#define OVERLAY_PARAM_CUSTOM(name)
1518a1362adSmaya   OVERLAY_PARAMS
1528a1362adSmaya#undef OVERLAY_PARAM_BOOL
1538a1362adSmaya#undef OVERLAY_PARAM_CUSTOM
1548a1362adSmaya};
1558a1362adSmaya
1568a1362adSmayavoid
1578a1362adSmayaparse_overlay_env(struct overlay_params *params,
1588a1362adSmaya                  const char *env)
1598a1362adSmaya{
1608a1362adSmaya   uint32_t num;
1618a1362adSmaya   char key[256], value[256];
1628a1362adSmaya
1638a1362adSmaya   memset(params, 0, sizeof(*params));
1648a1362adSmaya
1658a1362adSmaya   /* Visible by default */
1668a1362adSmaya   params->enabled[OVERLAY_PARAM_ENABLED_fps] = true;
1678a1362adSmaya   params->enabled[OVERLAY_PARAM_ENABLED_frame_timing] = true;
1687ec681f3Smrg   params->enabled[OVERLAY_PARAM_ENABLED_device] = true;
1697ec681f3Smrg   params->enabled[OVERLAY_PARAM_ENABLED_format] = true;
1708a1362adSmaya   params->fps_sampling_period = 500000; /* 500ms */
1718a1362adSmaya   params->width = params->height = 300;
1727ec681f3Smrg   params->control = -1;
1738a1362adSmaya
1748a1362adSmaya   if (!env)
1758a1362adSmaya      return;
1768a1362adSmaya
1778a1362adSmaya   while ((num = parse_string(env, key, value)) != 0) {
1788a1362adSmaya      env += num;
1798a1362adSmaya
1808a1362adSmaya#define OVERLAY_PARAM_BOOL(name)                                        \
1818a1362adSmaya      if (!strcmp(#name, key)) {                                        \
1828a1362adSmaya         params->enabled[OVERLAY_PARAM_ENABLED_##name] =                \
1838a1362adSmaya            strtol(value, NULL, 0);                                     \
1848a1362adSmaya         continue;                                                      \
1858a1362adSmaya      }
1868a1362adSmaya#define OVERLAY_PARAM_CUSTOM(name)               \
1878a1362adSmaya      if (!strcmp(#name, key)) {                 \
1888a1362adSmaya         params->name = parse_##name(value);     \
1898a1362adSmaya         continue;                               \
1908a1362adSmaya      }
1918a1362adSmaya      OVERLAY_PARAMS
1928a1362adSmaya#undef OVERLAY_PARAM_BOOL
1938a1362adSmaya#undef OVERLAY_PARAM_CUSTOM
1948a1362adSmaya      fprintf(stderr, "Unknown option '%s'\n", key);
1958a1362adSmaya   }
1968a1362adSmaya}
197