[Mesa-dev] [PATCH 2/5] glxinfo/wglinfo: move argument parsing into common code

Brian Paul brianp at vmware.com
Tue Jul 15 07:39:45 PDT 2014


---
 src/wgl/wglinfo.c          |   59 +++---------------------------
 src/xdemos/glinfo_common.c |   68 +++++++++++++++++++++++++++++++++++
 src/xdemos/glinfo_common.h |   25 +++++++++++++
 src/xdemos/glxinfo.c       |   85 ++++++++------------------------------------
 4 files changed, 113 insertions(+), 124 deletions(-)

diff --git a/src/wgl/wglinfo.c b/src/wgl/wglinfo.c
index e14ebd6..fe94dcc 100644
--- a/src/wgl/wglinfo.c
+++ b/src/wgl/wglinfo.c
@@ -42,14 +42,6 @@
 #include "glinfo_common.h"
 
 
-typedef enum
-{
-   Normal,
-   Wide,
-   Verbose
-} InfoMode;
-
-
 static GLboolean have_WGL_ARB_pixel_format;
 static GLboolean have_WGL_ARB_multisample;
 
@@ -534,67 +526,26 @@ find_best_visual(HDC hdc)
 }
 
 
-static void
-usage(void)
-{
-   printf("Usage: glxinfo [-v] [-t] [-h] [-i] [-b] [-display <dname>]\n");
-   printf("\t-v: Print visuals info in verbose form.\n");
-   printf("\t-t: Print verbose table.\n");
-   printf("\t-h: This information.\n");
-   printf("\t-b: Find the 'best' visual and print its number.\n");
-   printf("\t-l: Print interesting OpenGL limits.\n");
-   printf("\t-s: Print a single extension per line.\n");
-}
-
 
 int
 main(int argc, char *argv[])
 {
    HDC hdc;
-   InfoMode mode = Normal;
-   GLboolean findBest = GL_FALSE;
-   GLboolean limits = GL_FALSE;
-   GLboolean singleLine = GL_FALSE;
-   int i;
+   struct options opts;
 
-   for (i = 1; i < argc; i++) {
-      if (strcmp(argv[i], "-t") == 0) {
-         mode = Wide;
-      }
-      else if (strcmp(argv[i], "-v") == 0) {
-         mode = Verbose;
-      }
-      else if (strcmp(argv[i], "-b") == 0) {
-         findBest = GL_TRUE;
-      }
-      else if (strcmp(argv[i], "-l") == 0) {
-         limits = GL_TRUE;
-      }
-      else if (strcmp(argv[i], "-h") == 0) {
-         usage();
-         return 0;
-      }
-      else if(strcmp(argv[i], "-s") == 0) {
-         singleLine = GL_TRUE;
-      }
-      else {
-         printf("Unknown option `%s'\n", argv[i]);
-         usage();
-         return 0;
-      }
-   }
+   parse_args(argc, argv, &opts);
 
    hdc = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL);
 
-   if (findBest) {
+   if (opts.findBest) {
       int b;
       b = find_best_visual(hdc);
       printf("%d\n", b);
    }
    else {
-      print_screen_info(hdc, limits, singleLine);
+      print_screen_info(hdc, opts.limits, opts.singleLine);
       printf("\n");
-      print_visual_info(hdc, mode);
+      print_visual_info(hdc, opts.mode);
    }
 
    return 0;
diff --git a/src/xdemos/glinfo_common.c b/src/xdemos/glinfo_common.c
index 248b937..95ef545 100644
--- a/src/xdemos/glinfo_common.c
+++ b/src/xdemos/glinfo_common.c
@@ -718,3 +718,71 @@ context_flags_string(int mask)
 }
 
 
+static void
+usage(void)
+{
+#ifdef _WIN32
+   printf("Usage: wglinfo [-v] [-t] [-h] [-b] [-l] [-s]\n");
+#else
+   printf("Usage: glxinfo [-v] [-t] [-h] [-b] [-l] [-s] [-i] [-display <dname>]\n");
+   printf("\t-display <dname>: Print GLX visuals on specified server.\n");
+   printf("\t-i: Force an indirect rendering context.\n");
+#endif
+   printf("\t-v: Print visuals info in verbose form.\n");
+   printf("\t-t: Print verbose table.\n");
+   printf("\t-h: This information.\n");
+   printf("\t-b: Find the 'best' visual and print its number.\n");
+   printf("\t-l: Print interesting OpenGL limits.\n");
+   printf("\t-s: Print a single extension per line.\n");
+}
+
+
+void
+parse_args(int argc, char *argv[], struct options *options)
+{
+   int i;
+
+   options->mode = Normal;
+   options->findBest = GL_FALSE;
+   options->limits = GL_FALSE;
+   options->singleLine = GL_FALSE;
+   options->displayName = NULL;
+   options->allowDirect = GL_FALSE;
+
+   for (i = 1; i < argc; i++) {
+#ifndef _WIN32
+      if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
+         options->displayName = argv[i + 1];
+         i++;
+      }
+      else if (strcmp(argv[i], "-i") == 0) {
+         options->allowDirect = GL_FALSE;
+      }
+      else
+#endif
+      if (strcmp(argv[i], "-t") == 0) {
+         options->mode = Wide;
+      }
+      else if (strcmp(argv[i], "-v") == 0) {
+         options->mode = Verbose;
+      }
+      else if (strcmp(argv[i], "-b") == 0) {
+         options->findBest = GL_TRUE;
+      }
+      else if (strcmp(argv[i], "-l") == 0) {
+         options->limits = GL_TRUE;
+      }
+      else if (strcmp(argv[i], "-h") == 0) {
+         usage();
+         exit(0);
+      }
+      else if(strcmp(argv[i], "-s") == 0) {
+         options->singleLine = GL_TRUE;
+      }
+      else {
+         printf("Unknown option `%s'\n", argv[i]);
+         usage();
+         exit(0);
+      }
+   }
+}
diff --git a/src/xdemos/glinfo_common.h b/src/xdemos/glinfo_common.h
index d0daf4d..a268727 100644
--- a/src/xdemos/glinfo_common.h
+++ b/src/xdemos/glinfo_common.h
@@ -63,6 +63,27 @@ struct bit_info
 };
 
 
+typedef enum
+{
+   Normal,
+   Wide,
+   Verbose
+} InfoMode;
+
+
+struct options
+{
+   InfoMode mode;
+   GLboolean findBest;
+   GLboolean limits;
+   GLboolean singleLine;
+   /* GLX only */
+   char *displayName;
+   GLboolean allowDirect;
+};
+
+
+
 void
 print_extension_list(const char *ext, GLboolean singleLine);
 
@@ -86,4 +107,8 @@ const char *
 context_flags_string(int mask);
 
 
+void
+parse_args(int argc, char *argv[], struct options *options);
+
+
 #endif /* GLINFO_COMMON_H */
diff --git a/src/xdemos/glxinfo.c b/src/xdemos/glxinfo.c
index 5208423..6e7fa3a 100644
--- a/src/xdemos/glxinfo.c
+++ b/src/xdemos/glxinfo.c
@@ -63,13 +63,6 @@
 #define GLX_COLOR_INDEX_BIT		0x00000002
 #endif
 
-typedef enum
-{
-   Normal,
-   Wide,
-   Verbose
-} InfoMode;
-
 
 struct visual_attribs
 {
@@ -1204,76 +1197,24 @@ find_best_visual(Display *dpy, int scrnum)
 }
 
 
-static void
-usage(void)
-{
-   printf("Usage: glxinfo [-v] [-t] [-h] [-i] [-b] [-s] [-display <dname>]\n");
-   printf("\t-v: Print visuals info in verbose form.\n");
-   printf("\t-t: Print verbose table.\n");
-   printf("\t-display <dname>: Print GLX visuals on specified server.\n");
-   printf("\t-h: This information.\n");
-   printf("\t-i: Force an indirect rendering context.\n");
-   printf("\t-b: Find the 'best' visual and print its number.\n");
-   printf("\t-l: Print interesting OpenGL limits.\n");
-   printf("\t-s: Print a single extension per line.\n");
-}
-
-
 int
 main(int argc, char *argv[])
 {
-   char *displayName = NULL;
    Display *dpy;
    int numScreens, scrnum;
-   InfoMode mode = Normal;
-   Bool findBest = False;
-   Bool limits = False;
-   Bool allowDirect = True;
-   Bool singleLine = False;
+   struct options opts;
    Bool coreWorked;
-   int i;
 
-   for (i = 1; i < argc; i++) {
-      if (strcmp(argv[i], "-display") == 0 && i + 1 < argc) {
-         displayName = argv[i + 1];
-         i++;
-      }
-      else if (strcmp(argv[i], "-t") == 0) {
-         mode = Wide;
-      }
-      else if (strcmp(argv[i], "-v") == 0) {
-         mode = Verbose;
-      }
-      else if (strcmp(argv[i], "-b") == 0) {
-         findBest = True;
-      }
-      else if (strcmp(argv[i], "-i") == 0) {
-         allowDirect = False;
-      }
-      else if (strcmp(argv[i], "-l") == 0) {
-         limits = True;
-      }
-      else if (strcmp(argv[i], "-h") == 0) {
-         usage();
-         return 0;
-      }
-      else if (strcmp(argv[i], "-s") == 0) {
-         singleLine = True;
-      }
-      else {
-         printf("Unknown option `%s'\n", argv[i]);
-         usage();
-         return 0;
-      }
-   }
+   parse_args(argc, argv, &opts);
 
-   dpy = XOpenDisplay(displayName);
+   dpy = XOpenDisplay(opts.displayName);
    if (!dpy) {
-      fprintf(stderr, "Error: unable to open display %s\n", XDisplayName(displayName));
+      fprintf(stderr, "Error: unable to open display %s\n",
+              XDisplayName(opts.displayName));
       return -1;
    }
 
-   if (findBest) {
+   if (opts.findBest) {
       int b;
       mesa_hack(dpy, 0);
       b = find_best_visual(dpy, 0);
@@ -1284,14 +1225,18 @@ main(int argc, char *argv[])
       print_display_info(dpy);
       for (scrnum = 0; scrnum < numScreens; scrnum++) {
          mesa_hack(dpy, scrnum);
-         coreWorked = print_screen_info(dpy, scrnum, allowDirect, True, False, limits, singleLine, False);
-         print_screen_info(dpy, scrnum, allowDirect, False, False, limits, singleLine, coreWorked);
-         print_screen_info(dpy, scrnum, allowDirect, False, True, False, singleLine, True);
+         coreWorked = print_screen_info(dpy, scrnum, opts.allowDirect,
+                                        True, False, opts.limits,
+                                        opts.singleLine, False);
+         print_screen_info(dpy, scrnum, opts.allowDirect, False, False,
+                           opts.limits, opts.singleLine, coreWorked);
+         print_screen_info(dpy, scrnum, opts.allowDirect, False, True, False,
+                           opts.singleLine, True);
 
          printf("\n");
-         print_visual_info(dpy, scrnum, mode);
+         print_visual_info(dpy, scrnum, opts.mode);
 #ifdef GLX_VERSION_1_3
-         print_fbconfig_info(dpy, scrnum, mode);
+         print_fbconfig_info(dpy, scrnum, opts.mode);
 #endif
          if (scrnum + 1 < numScreens)
             printf("\n\n");
-- 
1.7.10.4



More information about the mesa-dev mailing list