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 		;
22 
usage()23 static void usage()
24 {
25 	puts(usage_str);
26 }
27 
main(int argc,char ** argv)28 int main(int argc, char **argv)
29 {
30 	string dev_path = "/dev/dri/card0";
31 
32 	vector<string> conn_strs;
33 	uint32_t time = 0;
34 
35 	OptionSet optionset = {
36 		Option("|device=", [&dev_path](string s)
37 		{
38 			dev_path = s;
39 		}),
40 		Option("c|connector=", [&conn_strs](string str)
41 		{
42 			conn_strs.push_back(str);
43 		}),
44 		Option("t|time=", [&time](string str)
45 		{
46 			time = stoul(str);
47 		}),
48 		Option("h|help", []()
49 		{
50 			usage();
51 			exit(-1);
52 		}),
53 	};
54 
55 	optionset.parse(argc, argv);
56 
57 	if (optionset.params().size() > 0) {
58 		usage();
59 		exit(-1);
60 	}
61 
62 	Card card(dev_path);
63 	ResourceManager resman(card);
64 
65 	vector<Connector*> conns;
66 
67 	if (conn_strs.size() > 0) {
68 		for (string s : conn_strs) {
69 			auto c = resman.reserve_connector(s);
70 			if (!c)
71 				EXIT("Failed to resolve connector '%s'", s.c_str());
72 			conns.push_back(c);
73 		}
74 	} else {
75 		conns = card.get_connectors();
76 	}
77 
78 	bool blank = true;
79 
80 	while (true) {
81 		for (Connector* conn : conns) {
82 			if (!conn->connected()) {
83 				printf("Connector %u not connected\n", conn->idx());
84 				continue;
85 			}
86 
87 			printf("Connector %u: %sblank\n", conn->idx(), blank ? "" : "un");
88 			int r = conn->set_prop_value("DPMS", blank ? 3 : 0);
89 			if (r)
90 				EXIT("Failed to set DPMS: %d", r);
91 		}
92 
93 		if (time == 0)
94 			break;
95 
96 		usleep(1000 * time);
97 
98 		blank = !blank;
99 	}
100 
101 	printf("press enter to exit\n");
102 
103 	getchar();
104 }
105