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