C++11 tidying.
[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 "warnings.h"
24 #include <dcp/raw_convert.h>
25 DCPOMATIC_DISABLE_WARNINGS
26 #include <libxml++/libxml++.h>
27 DCPOMATIC_ENABLE_WARNINGS
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string.hpp>
30 #include <iostream>
31
32 #define EDID_SYS_PATH "/sys/class/drm"
33 static uint8_t const edid_header[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
34
35 using std::vector;
36 using std::string;
37 using std::cout;
38
39 vector<Monitor>
40 get_monitors()
41 {
42         using namespace boost::filesystem;
43         using namespace boost::algorithm;
44
45         vector<Monitor> monitors;
46
47         int card = 0;
48         while (true) {
49                 path card_dir = String::compose("%1/card%2", EDID_SYS_PATH, card);
50                 if (!is_directory(card_dir)) {
51                         break;
52                 }
53
54                 for (directory_iterator i = directory_iterator(card_dir); i != directory_iterator(); ++i) {
55                         if (!starts_with(i->path().filename().string(), String::compose("card%1", card))) {
56                                 continue;
57                         }
58
59                         FILE* edid_file = fopen(path(i->path() / "edid").string().c_str(), "r");
60                         if (!edid_file) {
61                                 continue;
62                         }
63
64                         uint8_t edid[128];
65                         int const N = fread(edid, 1, sizeof(edid), edid_file);
66                         fclose(edid_file);
67                         if (N != 128) {
68                                 continue;
69                         }
70
71                         if (memcmp(edid, edid_header, 8) != 0) {
72                                 continue;
73                         }
74
75                         Monitor mon;
76
77                         uint16_t mid = (edid[8] << 8) | edid[9];
78                         mon.manufacturer_id += char(((mid >> 10) & 0x1f) + 'A' - 1);
79                         mon.manufacturer_id += char(((mid >> 5) & 0x1f) + 'A' - 1);
80                         mon.manufacturer_id += char(((mid >> 0) & 0x1f) + 'A' - 1);
81
82                         mon.manufacturer_product_code = (edid[11] << 8) | edid[10];
83
84                         mon.serial_number = (edid[15] << 24) | (edid[14] << 16) | (edid[13] << 8) | edid[12];
85                         mon.week_of_manufacture = edid[16];
86                         mon.year_of_manufacture = edid[17];
87                         monitors.push_back (mon);
88                 }
89
90                 ++card;
91         }
92
93         return monitors;
94 }
95
96 Monitor::Monitor ()
97         : manufacturer_product_code (0)
98         , serial_number (0)
99         , week_of_manufacture (0)
100         , year_of_manufacture (0)
101 {
102
103 }
104
105 Monitor::Monitor (cxml::ConstNodePtr node)
106         : manufacturer_id(node->string_child("ManufacturerId"))
107         , manufacturer_product_code(node->number_child<uint32_t>("ManufacturerProductCode"))
108         , serial_number(node->number_child<uint32_t>("SerialNumber"))
109         , week_of_manufacture(node->number_child<int>("WeekOfManufacture"))
110         , year_of_manufacture(node->number_child<int>("YearOfManufacture"))
111
112 {
113
114 }
115
116 void
117 Monitor::as_xml (xmlpp::Node* parent) const
118 {
119         parent->add_child("ManufacturerId")->add_child_text(manufacturer_id);
120         parent->add_child("ManufacturerProductCode")->add_child_text(dcp::raw_convert<string>(manufacturer_product_code));
121         parent->add_child("SerialNumber")->add_child_text(dcp::raw_convert<string>(serial_number));
122         parent->add_child("WeekOfManufacture")->add_child_text(dcp::raw_convert<string>(week_of_manufacture));
123         parent->add_child("YearOfManufacture")->add_child_text(dcp::raw_convert<string>(year_of_manufacture));
124 }
125
126 bool
127 operator== (Monitor const & a, Monitor const & b)
128 {
129         return a.manufacturer_id == b.manufacturer_id &&
130                 a.manufacturer_product_code == b.manufacturer_product_code &&
131                 a.serial_number == b.serial_number &&
132                 a.week_of_manufacture == b.week_of_manufacture &&
133                 a.year_of_manufacture == b.year_of_manufacture;
134 }