~lukas-kde/qtmir/betterSessionManagement

« back to all changes in this revision

Viewing changes to src/platforms/mirserver/miral/edid.cpp

  • Committer: Lukáš Tinkl
  • Date: 2017-03-24 11:51:00 UTC
  • mfrom: (590.1.35 qtmir)
  • Revision ID: lukas.tinkl@canonical.com-20170324115100-n0itqdupc26qe6g6
merge trunk, resolve conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Nick Dedekind <nick.dedekind@canonical.com>
 
17
 */
 
18
 
 
19
#include "edid.h"
 
20
 
 
21
#include <cstring>
 
22
#include <numeric>
 
23
#include <stdexcept>
 
24
 
 
25
miral::Edid& miral::Edid::parse_data(std::vector<uint8_t> const& data)
 
26
{
 
27
    if (data.size() != 128 && data.size() != 256) {
 
28
        throw std::runtime_error("Incorrect EDID structure size");
 
29
    }
 
30
 
 
31
    // check the checksum
 
32
    uint8_t sum = std::accumulate(data.begin(), data.end(), 0);
 
33
    if (sum != 0) {
 
34
        throw std::runtime_error("Invalid EDID checksum");
 
35
    }
 
36
 
 
37
    // check header
 
38
    static constexpr uint8_t sig[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
 
39
    if (std::memcmp(data.data(), sig, sizeof sig)) {
 
40
        throw std::runtime_error("Invalid EDID header");
 
41
    }
 
42
 
 
43
    vendor = { (char)((data[8] >> 2 & 0x1f) + 'A' - 1),
 
44
               (char)((((data[8] & 0x3) << 3) | ((data[9] & 0xe0) >> 5)) + 'A' - 1),
 
45
               (char)((data[9] & 0x1f) + 'A' - 1) };
 
46
 
 
47
    product_code = *(uint16_t*)&data[10];
 
48
    serial_number = *(uint32_t*)&data[12];
 
49
 
 
50
    size.width = (int)data[21] * 10;
 
51
    size.height = (int)data[22] * 10;
 
52
 
 
53
    for (int descriptor_index = 0, i = 54; descriptor_index < 4; descriptor_index++, i += 18) { //read through descriptor blocks... 54-71, 72-89, 90-107, 108-125
 
54
        if (data[i] == 0x00) { //not a timing descriptor
 
55
 
 
56
            auto& descriptor = descriptors[descriptor_index];
 
57
            auto& value = descriptor.value;
 
58
 
 
59
            descriptor.type = static_cast<Edid::Descriptor::Type>(data[i+3]);
 
60
 
 
61
            switch (descriptor.type) {
 
62
            case Descriptor::Type::monitor_name:
 
63
                for (int j = 5; j < 18; j++) {
 
64
                    if (data[i+j] == 0x0a) {
 
65
                        break;
 
66
                    } else {
 
67
                        value.monitor_name[j-5] = data[i+j];
 
68
                    }
 
69
                }
 
70
                break;
 
71
            case Descriptor::Type::monitor_limits:
 
72
                break;
 
73
            case Descriptor::Type::unspecified_text:
 
74
                for (int j = 5; j < 18; j++) {
 
75
                    if (data[i+j] == 0x0a) {
 
76
                        break;
 
77
                    } else {
 
78
                        value.unspecified_text[j-5] = data[i+j];
 
79
                    }
 
80
                }
 
81
                break;
 
82
            case Descriptor::Type::serial_number:
 
83
                for (int j = 5; j < 18; j++) {
 
84
                    if (data[i+j] == 0x0a) {
 
85
                        break;
 
86
                    } else {
 
87
                        value.serial_number[j-5] = data[i+j];
 
88
                    }
 
89
                }
 
90
                break;
 
91
            default:
 
92
                break;
 
93
            }
 
94
        }
 
95
    }
 
96
 
 
97
    return *this;
 
98
}
 
99
 
 
100
std::string miral::Edid::Descriptor::string_value() const
 
101
{
 
102
    switch(type) {
 
103
    case Type::monitor_name:
 
104
        return std::string(value.monitor_name);
 
105
    case Type::unspecified_text:
 
106
        return std::string(value.unspecified_text);
 
107
    case Type::serial_number:
 
108
        return std::string(value.serial_number);
 
109
    default:
 
110
        return {};
 
111
    }
 
112
}