kmsprint: use the new option parser
authorTomi Valkeinen <tomi.valkeinen@ti.com>
Thu, 26 Nov 2015 16:07:36 +0000 (18:07 +0200)
committerTomi Valkeinen <tomi.valkeinen@ti.com>
Thu, 26 Nov 2015 20:45:18 +0000 (22:45 +0200)
tests/CMakeLists.txt
tests/kmsprint.cpp

index 9f6a60c6b784a6b5f98430fbc004845e22d0fb23..092220f805e81bfd305b3a43157858ba601eb64e 100644 (file)
@@ -11,6 +11,6 @@ add_executable (kmsview kmsview.cpp)
 target_link_libraries(kmsview kms++ kmstest ${LIBDRM_LIBRARIES})
 
 add_executable (kmsprint kmsprint.cpp)
-target_link_libraries(kmsprint kms++ ${LIBDRM_LIBRARIES})
+target_link_libraries(kmsprint kms++ kmstest ${LIBDRM_LIBRARIES})
 
 add_executable (optiontester optiontester.cpp)
index 697506b1e3dac2a602439c3e076dfe4aa4cd739b..7b9de8c0f76c2fe18b7584fe5ca73a939a08d47d 100644 (file)
@@ -3,13 +3,19 @@
 #include <iostream>
 
 #include "kms++.h"
-#include "cmdoptions.h"
+#include "opts.h"
 
 using namespace std;
 using namespace kms;
 
 namespace kmsprint {
 
+static struct {
+       bool print_props;
+       bool print_modes;
+       bool recurse;
+} opts;
+
 string width(int w, string str)
 {
        str.resize(w, ' ');
@@ -54,7 +60,7 @@ void print_properties(DrmObject& o, int ind)
        }
 }
 
-void print_plane(Plane& p, int ind, const CmdOptions& opts)
+void print_plane(Plane& p, int ind)
 {
        printf("%sPlane Id %d %d,%d -> %dx%d formats:", width(ind, "").c_str(),
               p.id(), p.crtc_x(), p.crtc_y(), p.x(), p.y());
@@ -62,11 +68,11 @@ void print_plane(Plane& p, int ind, const CmdOptions& opts)
                printf(" %s", PixelFormatToFourCC(f).c_str());
        printf("\n");
 
-       if (opts.is_set("p"))
+       if (opts.print_props)
                print_properties(p, ind+2);
 }
 
-void print_crtc(Crtc& cc, int ind, const CmdOptions& opts)
+void print_crtc(Crtc& cc, int ind)
 {
        printf("%sCRTC Id %d BufferId %d %dx%d at %dx%d gamma_size %d\n",
               width(ind, "").c_str(), cc.id(), cc.buffer_id(), cc.width(),
@@ -75,28 +81,28 @@ void print_crtc(Crtc& cc, int ind, const CmdOptions& opts)
        printf("%s   Mode ", width(ind, "").c_str());
        print_mode(cc.mode(), 0);
 
-       if (opts.is_set("p"))
+       if (opts.print_props)
                print_properties(cc, ind+2);
 
-       if (opts.is_set("r"))
+       if (opts.recurse)
                for (auto p : cc.get_possible_planes())
-                       print_plane(*p, ind + 2, opts);
+                       print_plane(*p, ind + 2);
 }
 
-void print_encoder(Encoder& e, int ind, const CmdOptions& opts)
+void print_encoder(Encoder& e, int ind)
 {
        printf("%sEncoder Id %d type %s\n", width(ind, "").c_str(),
               e.id(), e.get_encoder_type().c_str());
 
-       if (opts.is_set("p"))
+       if (opts.print_props)
                print_properties(e, ind+2);
 
-       if (opts.is_set("r"))
+       if (opts.recurse)
                for (auto cc : e.get_possible_crtcs())
-                       print_crtc(*cc, ind + 2, opts);
+                       print_crtc(*cc, ind + 2);
 }
 
-void print_connector(Connector& c, int ind, const CmdOptions& opts)
+void print_connector(Connector& c, int ind)
 {
        printf("%sConnector %s Id %d %sconnected", width(ind, "").c_str(),
               c.fullname().c_str(), c.id(), c.connected() ? "" : "dis");
@@ -104,14 +110,14 @@ void print_connector(Connector& c, int ind, const CmdOptions& opts)
                printf(" Subpixel: %s", c.subpixel_str().c_str());
        printf("\n");
 
-       if (opts.is_set("p"))
+       if (opts.print_props)
                print_properties(c, ind+2);
 
-       if (opts.is_set("r"))
+       if (opts.recurse)
                for (auto enc : c.get_encoders())
-                       print_encoder(*enc, ind + 2, opts);
+                       print_encoder(*enc, ind + 2);
 
-       if (opts.is_set("m")) {
+       if (opts.print_modes) {
                auto modes = c.get_modes();
                printf("%sModes, %u in total:\n", width(ind + 2, "").c_str(),
                       (unsigned) modes.size());
@@ -122,51 +128,90 @@ void print_connector(Connector& c, int ind, const CmdOptions& opts)
 
 }
 
-static map<string, CmdOption> options = {
-       { "-id", HAS_PARAM("Object id to print") },
-       { "p", NO_PARAM("Print properties") },
-       { "m", NO_PARAM("Print modes") },
-       { "r", NO_PARAM("Recursively print all related objects") },
-};
-
 using namespace kmsprint;
 
-int main(int argc, char **argv)
+static const char* usage_str =
+               "Usage: kmsprint [OPTIONS]\n\n"
+               "Options:\n"
+               "  -m, --modes       Print modes\n"
+               "  -p, --props       Print properties\n"
+               "  -r, --recurse     Recursively print all related objects\n"
+               "      --id=<ID>     Print object <ID>\n"
+               ;
+
+static void usage()
 {
-       Card card;
-       CmdOptions opts(argc, argv, options);
+       puts(usage_str);
+}
 
-       if (opts.error().length()) {
-               cerr << opts.error() << opts.usage();
-               return -1;
+int main(int argc, char **argv)
+{
+       string dev_path;
+       unsigned id = 0;
+
+       OptionSet optionset = {
+               Option("|device=",
+               [&](string s)
+               {
+                       dev_path = s;
+               }),
+               Option("|id=",
+               [&](string s)
+               {
+                       id = stoul(s);
+               }),
+               Option("p", [&](string s)
+               {
+                       opts.print_props = true;
+               }),
+               Option("m", [&](string s)
+               {
+                       opts.print_modes = true;
+               }),
+               Option("r", [&](string s)
+               {
+                       opts.recurse = true;
+               }),
+               Option("h|help", [&]()
+               {
+                       usage();
+                       exit(-1);
+               }),
+       };
+
+       optionset.parse(argc, argv);
+
+       if (optionset.params().size() > 0) {
+               usage();
+               exit(-1);
        }
 
-       /* No options implyles recursion */
-       if (!opts.is_set("-id")) {
-               opts.get_option("r").oset();
+       Card card;
+
+       /* No options impliles recursion */
+       if (id == 0) {
+               opts.recurse = true;
                for (auto conn : card.get_connectors())
-                       print_connector(*conn, 0, opts);
+                       print_connector(*conn, 0);
                return 0;
-       }
-
-       if (opts.is_set("-id")) {
-               auto ob = card.get_object(atoi(opts.opt_param("-id").c_str()));
+       } else {
+               auto ob = card.get_object(id);
                if (!ob) {
-                       cerr << opts.cmd() << ": Object id " <<
-                               opts.opt_param("-id") << " not found." << endl;
+                       cerr << "kmsprint" << ": Object id " <<
+                               id << " not found." << endl;
                        return -1;
                }
 
                if (auto co = dynamic_cast<Connector*>(ob))
-                       print_connector(*co, 0, opts);
+                       print_connector(*co, 0);
                else if (auto en = dynamic_cast<Encoder*>(ob))
-                       print_encoder(*en, 0, opts);
+                       print_encoder(*en, 0);
                else if (auto cr = dynamic_cast<Crtc*>(ob))
-                       print_crtc(*cr, 0, opts);
+                       print_crtc(*cr, 0);
                else if (auto pl = dynamic_cast<Plane*>(ob))
-                       print_plane(*pl, 0, opts);
+                       print_plane(*pl, 0);
                else {
-                       cerr << opts.cmd() << ": Unkown DRM Object type" <<
+                       cerr << "kmsprint" << ": Unkown DRM Object type" <<
                                endl;
                        return -1;
                }