[waffle] [PATCH 06/12] wflinfo: add option for JSON output
With "-f json" wflinfo will now print the result of waffle_display_info_json(). Signed-off-by: Frank Henigman --- src/utils/wflinfo.c | 40 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/utils/wflinfo.c b/src/utils/wflinfo.c index 268d4b8..58f5688 100644 --- a/src/utils/wflinfo.c +++ b/src/utils/wflinfo.c @@ -77,6 +77,9 @@ static const char *usage_message = "-v, --verbose\n" "Print more information.\n" "\n" +"-f, --format\n" +"One of: original (default) or json\n" +"\n" "--forward-compatible\n" "Create a forward-compatible context.\n" "\n" @@ -99,6 +102,7 @@ enum { OPT_API = 'a', OPT_VERSION = 'V', OPT_PROFILE, +OPT_FORMAT = 'f', OPT_VERBOSE = 'v', OPT_DEBUG_CONTEXT, OPT_FORWARD_COMPATIBLE, @@ -110,6 +114,7 @@ static const struct option get_opts[] = { { .name = "api",.has_arg = required_argument, .val = OPT_API }, { .name = "version",.has_arg = required_argument, .val = OPT_VERSION }, { .name = "profile",.has_arg = required_argument, .val = OPT_PROFILE }, +{ .name = "format", .has_arg = required_argument, .val = OPT_FORMAT }, { .name = "verbose",.has_arg = no_argument, .val = OPT_VERBOSE }, { .name = "debug-context", .has_arg = no_argument, .val = OPT_DEBUG_CONTEXT }, { .name = "forward-compatible", .has_arg = no_argument, .val = OPT_FORWARD_COMPATIBLE }, @@ -248,6 +253,8 @@ struct options { int context_major; int context_minor; +enum format { FORMAT_ORIGINAL, FORMAT_JSON } format; + bool verbose; bool context_forward_compatible; @@ -330,12 +337,13 @@ parse_args(int argc, char *argv[], struct options *opts) opts->context_profile = WAFFLE_NONE; opts->context_major = WAFFLE_DONT_CARE; opts->context_minor = WAFFLE_DONT_CARE; +opts->format = FORMAT_ORIGINAL; // prevent getopt_long from printing an error message opterr = 0; while (loop_get_opt) { -int opt = getopt_long(argc, argv, "a:hp:vV:", get_opts, NULL); +int opt = getopt_long(argc, argv, "a:f:hp:vV:", get_opts, NULL); switch (opt) { case -1: loop_get_opt = false; @@ -384,6 +392,16 @@ parse_args(int argc, char *argv[], struct options *opts) optarg); } break; +case OPT_FORMAT: +if (strcmp(optarg, "original") == 0) { +opts->format = FORMAT_ORIGINAL; +} else if (strcmp(optarg, "json") == 0) { +opts->format = FORMAT_JSON; +} else { +usage_error_printf("'%s' is not a valid format", + optarg); +} +break; case OPT_VERBOSE: opts->verbose = true; break; @@ -1119,9 +1137,23 @@ main(int argc, char **argv) glGetStringi = waffle_get_proc_address("glGetStringi"); } -ok = print_wflinfo(&opts); -if (!ok) -error_waffle(); +char *info; +switch (opts.format) { +case FORMAT_ORIGINAL: +ok = print_wflinfo(&opts); +if (!ok) +error_waffle(); +break; + +case FORMAT_JSON: +info = waffle_display_info_json(dpy, false); +if (info) { +printf("%s\n", info); +free(info); +} else +error_waffle(); +break; +} ok = waffle_make_current(dpy, NULL, NULL); if (!ok) -- 2.6.0.rc2.230.g3dd15c0 ___ waffle mailing list waffle@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/waffle
Re: [waffle] [PATCH 06/12] wflinfo: add option for JSON output
On 01/06/2016 11:56 AM, Frank Henigman wrote: > With "-f json" wflinfo will now print the result of > waffle_display_info_json(). > > Signed-off-by: Frank Henigman > --- > src/utils/wflinfo.c | 40 > 1 file changed, 36 insertions(+), 4 deletions(-) > > diff --git a/src/utils/wflinfo.c b/src/utils/wflinfo.c > index 268d4b8..58f5688 100644 > --- a/src/utils/wflinfo.c > +++ b/src/utils/wflinfo.c > @@ -1119,9 +1137,23 @@ main(int argc, char **argv) > glGetStringi = waffle_get_proc_address("glGetStringi"); > } > > -ok = print_wflinfo(&opts); > -if (!ok) > -error_waffle(); > +char *info; > +switch (opts.format) { > +case FORMAT_ORIGINAL: > +ok = print_wflinfo(&opts); > +if (!ok) > +error_waffle(); > +break; > + > +case FORMAT_JSON: > +info = waffle_display_info_json(dpy, false); > +if (info) { > +printf("%s\n", info); > +free(info); > +} else > +error_waffle(); > +break; > +} I agree with the patch series's goals: teach wflinfo to print platform-specific info (such as EGL info); extract that info inside libwaffle and expose it through public API; that public API should provide JSON, and perhaps additional formats. But this patch 6/12 poses a problem. Post-patch, wflinfo can print its info in the old format and in a json format. But, depending on the output format, wflinfo uses completely separate code paths to obtain the information. The actual information that wflinfo provides, and the method by which it obtains that info, should be largely independent of the output format. When the user chooses an output format, that choice should affect the *presentation* but not fundamentally affect the *content*. So... For wflinfo's sake, we need a public waffle_get_my_info() function that returns data that can be translated into the old format and into the json format, or perhaps returns the old format and json directly. To move forward with this patch series, I see the following options: 1. waffle_get_my_info() returns only json I don't like this. This would require that wflinfo decode the json in order to provide the old format output. I would like to avoid decoding json in Waffle though, because that would require either (a) Waffle contain a json decoder implementation or (b) Waffle rely on some json library. As for (a), I don't want to maintain a json-decoder (a json *encoder*, though, I don't object to). As for (b), Waffle is such a small, focused project that it feels wrong to pull in a json-decoder library as a dependency. BUT, maybe this option could work and I'm overestimating the maintenance overhead of decoding json. 2. waffle_get_my_info() returns only some-other-well-defined-format-FOO I don't like this either. Just like option 1, it would require that wflinfo decode the FOO format in order to provide json output. 3. waffle_get_my_info() can return a json string or a string in the old wflinfo format If option 3 can be implemented cleanly, I think it's the best choice. This option eliminates the need for decoding any special format. Perhaps we could implement this by defining a private struct... struct wcore_display_info { struct { char *platform; char *api; } waffle; struct { char *vendor; char *renderer; ... char **extensions; } opengl; struct { char *version; char *vendor; char *client_apis; ... } egl; ... }; ...that waffle_get_my_info() populates and then encodes into the respected format: JSON, the old wflinfo format, a format that mimics glxinfo, or whatever. Frank, what do you think? ___ waffle mailing list waffle@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/waffle
Re: [waffle] [PATCH 06/12] wflinfo: add option for JSON output
On Fri, Apr 8, 2016 at 8:14 PM, Chad Versace wrote: > On 01/06/2016 11:56 AM, Frank Henigman wrote: >> With "-f json" wflinfo will now print the result of >> waffle_display_info_json(). >> >> Signed-off-by: Frank Henigman >> --- >> src/utils/wflinfo.c | 40 >> 1 file changed, 36 insertions(+), 4 deletions(-) >> >> diff --git a/src/utils/wflinfo.c b/src/utils/wflinfo.c >> index 268d4b8..58f5688 100644 >> --- a/src/utils/wflinfo.c >> +++ b/src/utils/wflinfo.c > > >> @@ -1119,9 +1137,23 @@ main(int argc, char **argv) >> glGetStringi = waffle_get_proc_address("glGetStringi"); >> } >> >> -ok = print_wflinfo(&opts); >> -if (!ok) >> -error_waffle(); >> +char *info; >> +switch (opts.format) { >> +case FORMAT_ORIGINAL: >> +ok = print_wflinfo(&opts); >> +if (!ok) >> +error_waffle(); >> +break; >> + >> +case FORMAT_JSON: >> +info = waffle_display_info_json(dpy, false); >> +if (info) { >> +printf("%s\n", info); >> +free(info); >> +} else >> +error_waffle(); >> +break; >> +} > > > I agree with the patch series's goals: teach wflinfo to print > platform-specific > info (such as EGL info); extract that info inside libwaffle and expose it > through public API; that public API should provide JSON, and perhaps > additional > formats. > > But this patch 6/12 poses a problem. Post-patch, wflinfo can print its info in > the old format and in a json format. But, depending on the output format, > wflinfo uses completely separate code paths to obtain the information. > > The actual information that wflinfo provides, and the method by which it > obtains that info, should be largely independent of the output format. When > the > user chooses an output format, that choice should affect the *presentation* > but > not fundamentally affect the *content*. > > So... > > For wflinfo's sake, we need a public waffle_get_my_info() function that > returns > data that can be translated into the old format and into the json format, or > perhaps > returns the old format and json directly. > > To move forward with this patch series, I see the following options: > > 1. waffle_get_my_info() returns only json > > I don't like this. This would require that wflinfo decode the json in > order to provide the old format output. I would like to avoid decoding > json in Waffle though, because that would require either (a) Waffle > contain a json decoder implementation or (b) Waffle rely on some json > library. As for (a), I don't want to maintain a json-decoder (a json > *encoder*, though, I don't object to). As for (b), Waffle is such > a small, focused project that it feels wrong to pull in a json-decoder > library as a dependency. > > BUT, maybe this option could work and I'm overestimating the maintenance > overhead of decoding json. > > 2. waffle_get_my_info() returns only some-other-well-defined-format-FOO > >I don't like this either. Just like option 1, it would require that >wflinfo decode the FOO format in order to provide json output. > > 3. waffle_get_my_info() can return a json string or a string in the old >wflinfo format > >If option 3 can be implemented cleanly, I think it's the best choice. >This option eliminates the need for decoding any special format. > >Perhaps we could implement this by defining a private struct... > > struct wcore_display_info { > struct { > char *platform; > char *api; > } waffle; > > struct { > char *vendor; > char *renderer; > ... > char **extensions; > } opengl; > > struct { > char *version; > char *vendor; > char *client_apis; > ... > } egl; > > ... > }; > > ...that waffle_get_my_info() populates and then encodes into the > respected format: JSON, the old wflinfo format, a format that > mimics glxinfo, or whatever. > > > Frank, what do you think? I really hope we can keep this simple. I mentioned in the cover letter to this series that the next step is to gut wflinfo and translate json to the old format. We can insert that after this patch if you like. Option 1 is the nicest by far, if the translator is written in python. It's just a few lines of code to read the json, plus a table that lists the correspondence between old format lines and json keys. So we have some really simple C code to call the waffle api and get the json, and a really simple python script to translate to old form
Re: [waffle] [PATCH 06/12] wflinfo: add option for JSON output
On Thu 21 Apr 2016, Frank Henigman wrote: > On Fri, Apr 8, 2016 at 8:14 PM, Chad Versace wrote: > > On 01/06/2016 11:56 AM, Frank Henigman wrote: > I really hope we can keep this simple. > > I mentioned in the cover letter to this series that the next step is > to gut wflinfo and translate json to the old format. We can insert > that after this patch if you like. Option 1 is the nicest by far, if > the translator is written in python. It's just a few lines of code to > read the json, plus a table that lists the correspondence between old > format lines and json keys. So we have some really simple C code to > call the waffle api and get the json, and a really simple python > script to translate to old format. I've got this much roughed out. > For maximum backward compatibility these two bits would be tied > together by a script that accepts all the options currently accepted > by wflinfo, i.e. we replace wflinfo with a script. Which for > portability is also written in python. Yes this introduces a > dependency on python to run wflinfo, but I doubt that's much of a > problem. I'm open to exploring that path. I just took a serious look at wflinfo.c, and it's needless complex for the little job that it does. If you could port it to Python, greatly reducing and simplifying the code, I'd like to see that. I've never used Python's C interface, so I look forward to learning about it from your patches. ___ waffle mailing list waffle@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/waffle