More player debugging for butler video-full states.
[dcpomatic.git] / src / lib / edid.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "compose.hpp"
22 #include "edid.h"
23 #include <dcp/raw_convert.h>
24 #include <libxml++/libxml++.h>
25 #include <boost/filesystem.hpp>
26 #include <boost/algorithm/string.hpp>
27 #include <iostream>
28
29 #define EDID_SYS_PATH "/sys/class/drm"
30 static uint8_t const edid_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
31
32 using std::vector;
33 using std::string;
34 using std::cout;
35
36 vector<Monitor>
37 get_monitors()
38 {
39         using namespace boost::filesystem;
40         using namespace boost::algorithm;
41
42         vector<Monitor> monitors;
43
44         int card = 0;
45         while (true) {
46                 path card_dir = String::compose("%1/card%2", EDID_SYS_PATH, card);
47                 if (!is_directory(card_dir)) {
48                         break;
49                 }
50
51                 for (directory_iterator i = directory_iterator(card_dir); i != directory_iterator(); ++i) {
52                         if (!starts_with(i->path().filename().string(), String::compose("card%1", card))) {
53                                 continue;
54                         }
55
56                         FILE* edid_file = fopen(path(i->path() / "edid").string().c_str(), "r");
57                         if (!edid_file) {
58                                 continue;
59                         }
60
61                         uint8_t edid[128];
62                         int const N = fread(edid, 1, sizeof(edid), edid_file);
63                         fclose(edid_file);
64                         if (N != 128) {
65                                 continue;
66                         }
67
68                         if (memcmp(edid, edid_header, 8) != 0) {
69                                 continue;
70                         }
71
72                         Monitor mon;
73
74                         uint16_t mid = (edid[8] << 8) | edid[9];
75                         mon.manufacturer_id += char(((mid >> 10) & 0x1f) + 'A' - 1);
76                         mon.manufacturer_id += char(((mid >> 5) & 0x1f) + 'A' - 1);
77                         mon.manufacturer_id += char(((mid >> 0) & 0x1f) + 'A' - 1);
78
79                         mon.manufacturer_product_code = (edid[11] << 8) | edid[10];
80
81                         mon.serial_number = (edid[15] << 24) | (edid[14] << 16) | (edid[13] << 8) | edid[12];
82                         mon.week_of_manufacture = edid[16];
83                         mon.year_of_manufacture = edid[17];
84                         monitors.push_back (mon);
85                 }
86
87                 ++card;
88         }
89
90         return monitors;
91 }
92
93 Monitor::Monitor ()
94         : manufacturer_product_code (0)
95         , serial_number (0)
96         , week_of_manufacture (0)
97         , year_of_manufacture (0)
98 {
99
100 }
101
102 Monitor::Monitor (cxml::ConstNodePtr node)
103         : manufacturer_id(node->string_child("ManufacturerId"))
104         , manufacturer_product_code(node->number_child<uint32_t>("ManufacturerProductCode"))
105         , serial_number(node->number_child<uint32_t>("SerialNumber"))
106         , week_of_manufacture(node->number_child<int>("WeekOfManufacture"))
107         , year_of_manufacture(node->number_child<int>("YearOfManufacture"))
108
109 {
110
111 }
112
113 void
114 Monitor::as_xml (xmlpp::Node* parent) const
115 {
116         parent->add_child("ManufacturerId")->add_child_text(manufacturer_id);
117         parent->add_child("ManufacturerProductCode")->add_child_text(dcp::raw_convert<string>(manufacturer_product_code));
118         parent->add_child("SerialNumber")->add_child_text(dcp::raw_convert<string>(serial_number));
119         parent->add_child("WeekOfManufacture")->add_child_text(dcp::raw_convert<string>(week_of_manufacture));
120         parent->add_child("YearOfManufacture")->add_child_text(dcp::raw_convert<string>(year_of_manufacture));
121 }
122
123 bool
124 operator== (Monitor const & a, Monitor const & b)
125 {
126         return a.manufacturer_id == b.manufacturer_id &&
127                 a.manufacturer_product_code == b.manufacturer_product_code &&
128                 a.serial_number == b.serial_number &&
129                 a.week_of_manufacture == b.week_of_manufacture &&
130                 a.year_of_manufacture == b.year_of_manufacture;
131 }