[PATCH vkd3d] vkd3d-compiler: Enable colour output by default when outputting to a colour capable tty.

Henri Verbeet hverbeet at codeweavers.com
Mon Sep 28 08:30:17 CDT 2020


Signed-off-by: Henri Verbeet <hverbeet at codeweavers.com>
---
 Makefile.am                    |  3 ++-
 configure.ac                   | 10 ++++++++--
 programs/vkd3d-compiler/main.c | 43 ++++++++++++++++++++++++++++++++++++------
 3 files changed, 47 insertions(+), 9 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index fe6ba926..07426fb4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -145,7 +145,8 @@ EXTRA_DIST += \
 
 bin_PROGRAMS = vkd3d-compiler
 vkd3d_compiler_SOURCES = programs/vkd3d-compiler/main.c
-vkd3d_compiler_LDADD = libvkd3d-shader.la
+vkd3d_compiler_CFLAGS = $(AM_CFLAGS) @NCURSES_CFLAGS@
+vkd3d_compiler_LDADD = libvkd3d-shader.la @NCURSES_LIBS@
 
 LDADD = libvkd3d.la libvkd3d-utils.la
 AM_DEFAULT_SOURCE_EXT = .c
diff --git a/configure.ac b/configure.ac
index 62093862..2f22b05f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -8,9 +8,10 @@ AC_CONFIG_HEADERS(include/config.h)
 AC_ARG_VAR([WIDL], [widl IDL compiler])
 AC_ARG_VAR([CROSSCC32], [32-bit Windows cross compiler])
 AC_ARG_VAR([CROSSCC64], [64-bit Windows cross compiler])
-AC_ARG_WITH([xcb], AS_HELP_STRING([--with-xcb], [Build with XCB library (default: test)]))
+AC_ARG_WITH([ncurses], AS_HELP_STRING([--with-ncurses], [Build with the ncurses library (default: test)]))
 AC_ARG_WITH([spirv-tools], AS_HELP_STRING([--with-spirv-tools],
                                           [Build with SPIRV-Tools library (default: disabled)]))
+AC_ARG_WITH([xcb], AS_HELP_STRING([--with-xcb], [Build with XCB library (default: test)]))
 AC_ARG_ENABLE([demos],
               AS_HELP_STRING([--enable-demos], [Build demo programs (default: disabled)]),,
               [enable_demos=no])
@@ -109,6 +110,10 @@ VKD3D_CHECK_SONAME([vulkan], [vkGetInstanceProcAddr],
                                                                                ["$ac_cv_lib_soname_MoltenVK"])],
                                                            [AC_MSG_ERROR([libvulkan and libMoltenVK not found.])])])])
 
+AS_IF([test "x$with_ncurses" != "xno"],
+      [PKG_CHECK_MODULES([NCURSES], [ncurses],
+                         [AC_DEFINE([HAVE_NCURSES], [1], [Define to 1 if you have ncurses.]) with_ncurses=yes],
+                         [with_ncurses=no])])
 AS_IF([test "x$with_spirv_tools" = "xyes"],
       [PKG_CHECK_MODULES([SPIRV_TOOLS], [SPIRV-Tools-shared],
       [AC_DEFINE([HAVE_SPIRV_TOOLS], [1], [Define to 1 if you have SPIRV-Tools.])])],
@@ -152,8 +157,9 @@ AS_ECHO(["
 
   widl: ${WIDL}
 
-  Have XCB: ${HAVE_XCB}
+  Have ncurses: ${with_ncurses}
   Have SPIRV-Tools: ${with_spirv_tools}
+  Have xcb: ${HAVE_XCB}
 
   Building demos: ${enable_demos}
   Building tests: ${enable_tests}
diff --git a/programs/vkd3d-compiler/main.c b/programs/vkd3d-compiler/main.c
index fdf3fafc..8480c4f8 100644
--- a/programs/vkd3d-compiler/main.c
+++ b/programs/vkd3d-compiler/main.c
@@ -17,9 +17,10 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
 #include <fcntl.h>
-#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
@@ -28,6 +29,9 @@
 
 #include "vkd3d_common.h"
 #include "vkd3d_shader.h"
+#ifdef HAVE_NCURSES
+#include <term.h>
+#endif
 
 #define MAX_COMPILE_OPTIONS 3
 
@@ -180,6 +184,8 @@ struct options
     bool print_version;
     bool print_source_types;
     bool print_target_types;
+    uint32_t formatting;
+    bool explicit_colour;
 
     struct vkd3d_shader_compile_option compile_options[MAX_COMPILE_OPTIONS];
     unsigned int compile_option_count;
@@ -230,7 +236,7 @@ static bool parse_buffer_uav(enum vkd3d_shader_compile_option_buffer_uav *buffer
     return false;
 }
 
-static bool parse_formatting(uint32_t *formatting, char *arg)
+static bool parse_formatting(uint32_t *formatting, bool *colour, char *arg)
 {
     static const struct formatting_option
     {
@@ -270,6 +276,8 @@ static bool parse_formatting(uint32_t *formatting, char *arg)
                     *formatting |= opts[i].value;
                 else
                     *formatting &= ~opts[i].value;
+                if (opts[i].value == VKD3D_SHADER_COMPILE_OPTION_FORMATTING_COLOUR)
+                    *colour = true;
                 break;
             }
         }
@@ -353,8 +361,6 @@ static bool validate_target_type(
 
 static bool parse_command_line(int argc, char **argv, struct options *options)
 {
-    enum vkd3d_shader_compile_option_formatting_flags formatting = VKD3D_SHADER_COMPILE_OPTION_FORMATTING_INDENT
-            | VKD3D_SHADER_COMPILE_OPTION_FORMATTING_HEADER;
     enum vkd3d_shader_compile_option_buffer_uav buffer_uav;
     int option;
 
@@ -374,6 +380,8 @@ static bool parse_command_line(int argc, char **argv, struct options *options)
     memset(options, 0, sizeof(*options));
     options->source_type = VKD3D_SHADER_SOURCE_DXBC_TPF;
     options->target_type = VKD3D_SHADER_TARGET_SPIRV_BINARY;
+    options->formatting = VKD3D_SHADER_COMPILE_OPTION_FORMATTING_INDENT
+            | VKD3D_SHADER_COMPILE_OPTION_FORMATTING_HEADER;
 
     for (;;)
     {
@@ -405,7 +413,7 @@ static bool parse_command_line(int argc, char **argv, struct options *options)
                 break;
 
             case OPTION_TEXT_FORMATTING:
-                if (!parse_formatting(&formatting, optarg))
+                if (!parse_formatting(&options->formatting, &options->explicit_colour, optarg))
                     return false;
                 break;
 
@@ -450,7 +458,6 @@ static bool parse_command_line(int argc, char **argv, struct options *options)
     if (options->print_target_types)
         return true;
 
-    add_compile_option(options, VKD3D_SHADER_COMPILE_OPTION_FORMATTING, formatting);
     if (optind < argc)
         options->filename = argv[argc - 1];
 
@@ -527,6 +534,26 @@ static FILE *open_output(const char *filename, bool *close)
     return f;
 }
 
+static bool has_colour(FILE *f)
+{
+#ifdef HAVE_NCURSES
+    bool supported;
+    int ret;
+
+    if (!isatty(fileno(f)))
+        return false;
+    setupterm(NULL, fileno(f), &ret);
+    if (ret != 1)
+        return false;
+    supported = !!tigetstr("setaf");
+    del_curterm(cur_term);
+
+    return supported;
+#else
+    return false;
+#endif
+}
+
 int main(int argc, char **argv)
 {
     bool close_input = false, close_output = false;
@@ -585,6 +612,10 @@ int main(int argc, char **argv)
         goto done;
     }
 
+    if (!options.explicit_colour && has_colour(output))
+        options.formatting |= VKD3D_SHADER_COMPILE_OPTION_FORMATTING_COLOUR;
+    add_compile_option(&options, VKD3D_SHADER_COMPILE_OPTION_FORMATTING, options.formatting);
+
     info.type = VKD3D_SHADER_STRUCTURE_TYPE_COMPILE_INFO;
     info.next = NULL;
     info.source_type = options.source_type;
-- 
2.11.0




More information about the wine-devel mailing list