xref: /aosp_15_r20/external/libkmsxx/utils/kmsblank.cpp (revision f0687c8a10b3e371dbe09214db6664e37c283cca)
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <algorithm>
4 
5 #include <kms++/kms++.h>
6 #include <kms++util/kms++util.h>
7 
8 using namespace std;
9 using namespace kms;
10 
11 static const char* usage_str =
12 	"Usage: kmsblank [OPTION]...\n\n"
13 	"Blank screen(s)\n\n"
14 	"Options:\n"
15 	"      --device=DEVICE       DEVICE is the path to DRM card to open\n"
16 	"  -c, --connector=CONN      CONN is <connector>\n"
17 	"  -t, --time=TIME           blank/unblank in TIME intervals\n"
18 	"\n"
19 	"<connector> can be given by index (<idx>) or id (@<id>).\n"
20 	"<connector> can also be given by name.\n";
21 
usage()22 static void usage()
23 {
24 	puts(usage_str);
25 }
26 
main(int argc,char ** argv)27 int main(int argc, char** argv)
28 {
29 	string dev_path;
30 
31 	vector<string> conn_strs;
32 	uint32_t time = 0;
33 
34 	OptionSet optionset = {
35 		Option("|device=", [&dev_path](string s) {
36 			dev_path = s;
37 		}),
38 		Option("c|connector=", [&conn_strs](string str) {
39 			conn_strs.push_back(str);
40 		}),
41 		Option("t|time=", [&time](string str) {
42 			time = stoul(str);
43 		}),
44 		Option("h|help", []() {
45 			usage();
46 			exit(-1);
47 		}),
48 	};
49 
50 	optionset.parse(argc, argv);
51 
52 	if (optionset.params().size() > 0) {
53 		usage();
54 		exit(-1);
55 	}
56 
57 	Card card(dev_path);
58 	ResourceManager resman(card);
59 
60 	vector<Connector*> conns;
61 
62 	if (conn_strs.size() > 0) {
63 		for (string s : conn_strs) {
64 			auto c = resman.reserve_connector(s);
65 			if (!c)
66 				EXIT("Failed to resolve connector '%s'", s.c_str());
67 			conns.push_back(c);
68 		}
69 	} else {
70 		conns = card.get_connectors();
71 	}
72 
73 	bool blank = true;
74 
75 	while (true) {
76 		for (Connector* conn : conns) {
77 			if (!conn->connected()) {
78 				printf("Connector %u not connected\n", conn->idx());
79 				continue;
80 			}
81 
82 			printf("Connector %u: %sblank\n", conn->idx(), blank ? "" : "un");
83 			int r = conn->set_prop_value("DPMS", blank ? 3 : 0);
84 			if (r)
85 				EXIT("Failed to set DPMS: %d", r);
86 		}
87 
88 		if (time == 0)
89 			break;
90 
91 		usleep(1000 * time);
92 
93 		blank = !blank;
94 	}
95 
96 	printf("press enter to exit\n");
97 
98 	getchar();
99 }
100