349266827ce832e5a3569bcbd2b4deeb075cfe78
[android/external-libkmsxx.git] / utils / kmsprint.cpp
1 #include <algorithm>
2 #include <cinttypes>
3 #include <cstdio>
4 #include <iostream>
5 #include <string>
6 #include <unistd.h>
8 #include <kms++/kms++.h>
9 #include <kms++util/kms++util.h>
11 using namespace std;
12 using namespace kms;
14 static struct {
15         bool print_props;
16         bool print_modes;
17         bool print_list;
18         bool x_modeline;
19 } s_opts;
21 static string format_mode(const Videomode& m, unsigned idx)
22 {
23         string str;
25         str = sformat("  %2u ", idx);
27         if (s_opts.x_modeline) {
28                 str += sformat("%12s %6u %4u %4u %4u %4u %4u %4u %4u %4u  %2u %#x %#x",
29                                m.name.c_str(),
30                                m.clock,
31                                m.hdisplay, m.hsync_start, m.hsync_end, m.htotal,
32                                m.vdisplay, m.vsync_start, m.vsync_end, m.vtotal,
33                                m.vrefresh,
34                                m.flags,
35                                m.type);
36         } else {
37                 string h = sformat("%u/%u/%u/%u", m.hdisplay, m.hfp(), m.hsw(), m.hbp());
38                 string v = sformat("%u/%u/%u/%u", m.vdisplay, m.vfp(), m.vsw(), m.vbp());
40                 str += sformat("%-12s %7.3f %-16s %-16s %2u (%.2f) %#10x %#6x",
41                                m.name.c_str(),
42                                m.clock / 1000.0,
43                                h.c_str(), v.c_str(),
44                                m.vrefresh, m.calculated_vrefresh(),
45                                m.flags,
46                                m.type);
47         }
49         return str;
50 }
52 static string format_mode_short(const Videomode& m)
53 {
54         string h = sformat("%u/%u/%u/%u", m.hdisplay, m.hfp(), m.hsw(), m.hbp());
55         string v = sformat("%u/%u/%u/%u", m.vdisplay, m.vfp(), m.vsw(), m.vbp());
57         return sformat("%s %.3f %s %s %u (%.2f)",
58                        m.name.c_str(),
59                        m.clock / 1000.0,
60                        h.c_str(), v.c_str(),
61                        m.vrefresh, m.calculated_vrefresh());
62 }
64 static string format_connector(Connector& c)
65 {
66         string str;
68         str = sformat("Connector %u (%u) %s",
69                       c.idx(), c.id(), c.fullname().c_str());
71         if (c.connected())
72                 str += " (connected)";
74         return str;
75 }
77 static string format_encoder(Encoder& e)
78 {
79         return sformat("Encoder %u (%u) %s",
80                        e.idx(), e.id(), e.get_encoder_type().c_str());
81 }
83 static string format_crtc(Crtc& c)
84 {
85         string str;
87         str = sformat("Crtc %u (%u)", c.idx(), c.id());
89         if (c.mode_valid())
90                 str += " " + format_mode_short(c.mode());
92         return str;
93 }
95 static string format_plane(Plane& p)
96 {
97         string str;
99         str = sformat("Plane %u (%u)", p.idx(), p.id());
101         if (p.fb_id())
102                 str += sformat(" fb-id: %u", p.fb_id());
104         string crtcs = join<Crtc*>(p.get_possible_crtcs(), " ", [](Crtc* crtc) { return to_string(crtc->idx()); });
106         str += sformat(" (crtcs: %s)", crtcs.c_str());
108         if (p.card().has_atomic()) {
109                 str += sformat(" %u,%u %ux%u -> %u,%u %ux%u",
110                                (uint32_t)p.get_prop_value("SRC_X") >> 16,
111                                (uint32_t)p.get_prop_value("SRC_Y") >> 16,
112                                (uint32_t)p.get_prop_value("SRC_W") >> 16,
113                                (uint32_t)p.get_prop_value("SRC_H") >> 16,
114                                (uint32_t)p.get_prop_value("CRTC_X"),
115                                (uint32_t)p.get_prop_value("CRTC_Y"),
116                                (uint32_t)p.get_prop_value("CRTC_W"),
117                                (uint32_t)p.get_prop_value("CRTC_H"));
118         }
120         string fmts = join<PixelFormat>(p.get_formats(), " ", [](PixelFormat fmt) { return PixelFormatToFourCC(fmt); });
122         str += sformat(" (%s)", fmts.c_str());
124         return str;
127 static string format_fb(Framebuffer& fb)
129         return sformat("FB %u %ux%u",
130                        fb.id(), fb.width(), fb.height());
133 static string format_property(const Property* prop, uint64_t val)
135         string ret = sformat("%s = ", prop->name().c_str());
137         switch (prop->type()) {
138         case PropertyType::Bitmask:
139         {
140                 vector<string> v, vall;
142                 for (auto kvp : prop->get_enums()) {
143                         if (val & (1 << kvp.first))
144                                 v.push_back(kvp.second);
145                         vall.push_back(sformat("%s=0x%x", kvp.second.c_str(), 1 << kvp.first));
146                 }
148                 ret += sformat("0x%" PRIx64 " (%s) [%s]", val, join(v, "|").c_str(), join(vall, "|").c_str());
150                 break;
151         }
153         case PropertyType::Blob:
154         {
155                 uint32_t blob_id = (uint32_t)val;
157                 if (blob_id) {
158                         Blob blob(prop->card(), blob_id);
159                         auto data = blob.data();
161                         ret += sformat("blob-id %u len %zu", blob_id, data.size());
162                 } else {
163                         ret += sformat("blob-id %u", blob_id);
164                 }
166                 break;
167         }
169         case PropertyType::Enum:
170         {
171                 string cur;
172                 vector<string> vall;
174                 for (auto kvp : prop->get_enums()) {
175                         if (val == kvp.first)
176                                 cur = kvp.second;
177                         vall.push_back(sformat("%s=%" PRIu64, kvp.second.c_str(), kvp.first));
178                 }
180                 ret += sformat("%" PRIu64 " (%s) [%s]", val, cur.c_str(), join(vall, "|").c_str());
182                 break;
183         }
185         case PropertyType::Object:
186         {
187                 ret += sformat("object id %u", (uint32_t)val);
188                 break;
189         }
191         case PropertyType::Range:
192         {
193                 auto values = prop->get_values();
195                 ret += sformat("%" PRIu64 " [%" PRIu64 " - %" PRIu64 "]",
196                                val, values[0], values[1]);
198                 break;
199         }
201         case PropertyType::SignedRange:
202         {
203                 auto values = prop->get_values();
205                 ret += sformat("%" PRIi64 " [%" PRIi64 " - %" PRIi64 "]",
206                                (int64_t)val, (int64_t)values[0], (int64_t)values[1]);
208                 break;
209         }
211         }
213         if (prop->is_pending())
214                 ret += " (pending)";
215         if (prop->is_immutable())
216                 ret += " (immutable)";
218         return ret;
221 static vector<string> format_props(DrmPropObject* o)
223         vector<string> lines;
225         auto pmap = o->get_prop_map();
226         for (auto pp : pmap) {
227                 const Property* p = o->card().get_prop(pp.first);
228                 lines.push_back(format_property(p, pp.second));
229         }
231         return lines;
234 static string format_ob(DrmObject* ob)
236         if (auto o = dynamic_cast<Connector*>(ob))
237                 return format_connector(*o);
238         else if (auto o = dynamic_cast<Encoder*>(ob))
239                 return format_encoder(*o);
240         else if (auto o = dynamic_cast<Crtc*>(ob))
241                 return format_crtc(*o);
242         else if (auto o = dynamic_cast<Plane*>(ob))
243                 return format_plane(*o);
244         else if (auto o = dynamic_cast<Framebuffer*>(ob))
245                 return format_fb(*o);
246         else
247                 EXIT("Unkown DRM Object type\n");
250 template<class T>
251 vector<T> filter(const vector<T>& sequence, function<bool(T)> predicate)
253         vector<T> result;
255         for(auto it = sequence.begin(); it != sequence.end(); ++it)
256                 if(predicate(*it))
257                         result.push_back(*it);
259         return result;
262 struct Entry
264         string title;
265         vector<string> lines;
266         vector<Entry> children;
267 };
269 static Entry& add_entry(vector<Entry>& entries)
271         entries.emplace_back();
272         return entries.back();
274 /*
275 static bool on_tty()
277         return isatty(STDOUT_FILENO) > 0;
279 */
280 enum class TreeGlyphMode {
281         None,
282         ASCII,
283         UTF8,
284 };
286 static TreeGlyphMode s_glyph_mode = TreeGlyphMode::None;
288 enum class TreeGlyph {
289         Vertical,
290         Branch,
291         Right,
292         Space,
293 };
295 static const map<TreeGlyph, string> glyphs_utf8 = {
296         { TreeGlyph::Vertical, "│ " },
297         { TreeGlyph::Branch, "├─" },
298         { TreeGlyph::Right, "└─" },
299         { TreeGlyph::Space, "  " },
301 };
303 static const map<TreeGlyph, string> glyphs_ascii = {
304         { TreeGlyph::Vertical, "| " },
305         { TreeGlyph::Branch, "|-" },
306         { TreeGlyph::Right, "`-" },
307         { TreeGlyph::Space, "  " },
309 };
311 const char* get_glyph(TreeGlyph glyph)
313         if (s_glyph_mode == TreeGlyphMode::None)
314                 return "  ";
316         const map<TreeGlyph, string>& glyphs = s_glyph_mode == TreeGlyphMode::UTF8 ? glyphs_utf8 : glyphs_ascii;
318         return glyphs.at(glyph).c_str();
321 static void print_entry(const Entry& e, const string& prefix, bool is_child, bool is_last)
323         string prefix1;
324         string prefix2;
326         if (is_child) {
327                 prefix1 = prefix + (is_last ? get_glyph(TreeGlyph::Right) : get_glyph(TreeGlyph::Branch));
328                 prefix2 = prefix + (is_last ? get_glyph(TreeGlyph::Space) : get_glyph(TreeGlyph::Vertical));
329         }
331         printf("%s%s\n", prefix1.c_str(), e.title.c_str());
333         bool has_children = e.children.size() > 0;
335         string data_prefix = prefix2 + (has_children ? get_glyph(TreeGlyph::Vertical) : get_glyph(TreeGlyph::Space));
337         for (const string& str : e.lines) {
338                 string p = data_prefix + get_glyph(TreeGlyph::Space);
339                 printf("%s%s\n", p.c_str(), str.c_str());
340         }
342         for (const Entry& child : e.children) {
343                 bool is_last = &child == &e.children.back();
345                 print_entry(child, prefix2, true, is_last);
346         }
349 static void print_entries(const vector<Entry>& entries, const string& prefix)
351         for (const Entry& e: entries) {
352                 print_entry(e, "", false, false);
353         }
356 template<class T>
357 static void append(vector<DrmObject*>& dst, const vector<T*>& src)
359         dst.insert(dst.end(), src.begin(), src.end());
363 static void print_as_list(Card& card)
365         vector<DrmPropObject*> obs;
366         vector<Framebuffer*> fbs;
368         for (Connector* conn : card.get_connectors()) {
369                 obs.push_back(conn);
370         }
372         for (Encoder* enc : card.get_encoders()) {
373                 obs.push_back(enc);
374         }
376         for (Crtc* crtc : card.get_crtcs()) {
377                 obs.push_back(crtc);
378                 if (crtc->buffer_id() && !card.has_has_universal_planes()) {
379                         Framebuffer* fb = new Framebuffer(card, crtc->buffer_id());
380                         fbs.push_back(fb);
381                 }
382         }
384         for (Plane* plane : card.get_planes()) {
385                 obs.push_back(plane);
386                 if (plane->fb_id()) {
387                         Framebuffer* fb = new Framebuffer(card, plane->fb_id());
388                         fbs.push_back(fb);
389                 }
390         }
392         for (DrmPropObject* ob: obs) {
393                 printf("%s\n", format_ob(ob).c_str());
395                 if (s_opts.print_props) {
396                         for (string str : format_props(ob))
397                                 printf("    %s\n", str.c_str());
398                 }
399         }
401         for (Framebuffer* fb: fbs) {
402                 printf("%s\n", format_ob(fb).c_str());
403         }
406 static void print_as_tree(Card& card)
408         vector<Entry> entries;
410         for (Connector* conn : card.get_connectors()) {
411                 if (!conn->connected())
412                         continue;
414                 Entry& e1 = add_entry(entries);
415                 e1.title = format_ob(conn);
416                 if (s_opts.print_props)
417                         e1.lines = format_props(conn);
419                 for (Encoder* enc : conn->get_encoders()) {
421                         Entry& e2 = add_entry(e1.children);
422                         e2.title = format_ob(enc);
423                         if (s_opts.print_props)
424                                 e2.lines = format_props(enc);
426                         if (Crtc* crtc = enc->get_crtc()) {
427                                 Entry& e3 = add_entry(e2.children);
428                                 e3.title = format_ob(crtc);
429                                 if (s_opts.print_props)
430                                         e3.lines = format_props(crtc);
432                                 if (crtc->buffer_id() && !card.has_has_universal_planes()) {
433                                         Framebuffer fb(card, crtc->buffer_id());
434                                         Entry& e5 = add_entry(e3.children);
436                                         e5.title = format_ob(&fb);
437                                 }
439                                 for (Plane* plane : card.get_planes()) {
440                                         if (plane->crtc_id() != crtc->id())
441                                                 continue;
443                                         Entry& e4 = add_entry(e3.children);
444                                         e4.title = format_ob(plane);
445                                         if (s_opts.print_props)
446                                                 e4.lines = format_props(plane);
448                                         uint32_t fb_id = plane->fb_id();
449                                         if (fb_id) {
450                                                 Framebuffer fb(card, fb_id);
452                                                 Entry& e5 = add_entry(e4.children);
454                                                 e5.title = format_ob(&fb);
455                                         }
456                                 }
457                         }
458                 }
459         }
461         print_entries(entries, "");
464 static void print_modes(Card& card)
466         for (Connector* conn : card.get_connectors()) {
467                 if (!conn->connected())
468                         continue;
470                 printf("%s\n", format_ob(conn).c_str());
472                 auto modes = conn->get_modes();
473                 for (unsigned i = 0; i < modes.size(); ++i)
474                         printf("%s\n", format_mode(modes[i], i).c_str());
475         }
478 static const char* usage_str =
479                 "Usage: kmsprint [OPTIONS]\n\n"
480                 "Options:\n"
481                 "  -l, --list        Print list instead of tree\n"
482                 "  -m, --modes       Print modes\n"
483                 "      --xmode       Print modes using X modeline\n"
484                 "  -p, --props       Print properties\n"
485                 ;
487 static void usage()
489         puts(usage_str);
492 int main(int argc, char **argv)
494         string dev_path = "/dev/dri/card0";
496         OptionSet optionset = {
497                 Option("|device=", [&dev_path](string s)
498                 {
499                         dev_path = s;
500                 }),
501                 Option("l|list", []()
502                 {
503                         s_opts.print_list = true;
504                 }),
505                 Option("m|modes", []()
506                 {
507                         s_opts.print_modes = true;
508                 }),
509                 Option("p|props", []()
510                 {
511                         s_opts.print_props = true;
512                 }),
513                 Option("|xmode", []() {
514                         s_opts.x_modeline = true;
515                 }),
516                 Option("h|help", []()
517                 {
518                         usage();
519                         exit(-1);
520                 }),
521         };
523         optionset.parse(argc, argv);
525         if (optionset.params().size() > 0) {
526                 usage();
527                 exit(-1);
528         }
530         Card card(dev_path);
532         if (s_opts.print_modes) {
533                 print_modes(card);
534                 return 0;
535         }
537         if (s_opts.print_list)
538                 print_as_list(card);
539         else
540                 print_as_tree(card);