File: color-edid.cpp

package info (click to toggle)
ukui-settings-daemon 4.0.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,324 kB
  • sloc: cpp: 39,120; ansic: 3,240; xml: 1,570; sh: 88; makefile: 4
file content (336 lines) | stat: -rw-r--r-- 9,337 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2023 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <QDebug>
#include <math.h>
#include <string.h>
#include <gio/gio.h>
#include <stdlib.h>

#include "color-edid.h"
#define EDID_OFFSET_PNPID                           0x08
#define EDID_OFFSET_SERIAL                          0x0c
#define EDID_OFFSET_SIZE                            0x15
#define EDID_OFFSET_GAMMA                           0x17
#define EDID_OFFSET_DATA_BLOCKS                     0x36
#define EDID_OFFSET_LAST_BLOCK                      0x6c
#define EDID_OFFSET_EXTENSION_BLOCK_COUNT           0x7e

#define DESCRIPTOR_DISPLAY_PRODUCT_NAME             0xfc
#define DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER    0xff
#define DESCRIPTOR_COLOR_MANAGEMENT_DATA            0xf9
#define DESCRIPTOR_ALPHANUMERIC_DATA_STRING         0xfe
#define DESCRIPTOR_COLOR_POINT                      0xfb

ColorEdid::ColorEdid()
{
    pnp_ids = gnome_pnp_ids_new ();
    pnp_id = g_new0 (gchar, 4);
    red = cd_color_yxy_new ();
    green = cd_color_yxy_new ();
    blue = cd_color_yxy_new ();
    white = cd_color_yxy_new ();
}


const gchar *ColorEdid::EdidGetMonitorName ()
{
    return monitor_name;
}

const gchar *ColorEdid::EdidGetVendorName ()
{
    if (vendor_name == NULL)
            vendor_name = gnome_pnp_ids_get_pnp_id (pnp_ids, pnp_id);
    return vendor_name;
}

const gchar *ColorEdid::EdidGetSerialNumber ()
{
    return serial_number;
}

const gchar *ColorEdid::EdidGetEisaId ()
{
    return eisa_id;
}

const gchar *ColorEdid::EdidGetChecksum ()
{
    return checksum;
}

const gchar *ColorEdid::EdidGetPnpId ()
{
    return pnp_id;
}

guint ColorEdid::EdidGetWidth ()
{
    return width;
}

guint ColorEdid::EdidGetHeight ()
{
    return height;
}

gfloat ColorEdid::EdidGetGamma ()
{
    return gamma;
}

const CdColorYxy *ColorEdid::EdidGetRed ()
{
    return red;
}

const CdColorYxy *ColorEdid::EdidGetGreen ()
{
    return green;
}

const CdColorYxy *ColorEdid::EdidGetBlue ()
{
    return blue;
}

const CdColorYxy *ColorEdid::EdidGetWhite ()
{
    return white;
}

void ColorEdid::EdidReset ()
{
    /* free old data */
    /*
    g_free (monitor_name);
    g_free (vendor_name);
    g_free (serial_number);
    g_free (eisa_id);
    g_free (checksum);
    */
    /* do not deallocate, just blank */
    pnp_id[0] = '\0';
    /* set to default values */
    monitor_name = NULL;
    vendor_name = NULL;
    serial_number = NULL;
    eisa_id = NULL;
    checksum = NULL;
    width = 0;
    height = 0;
    gamma = 0.0f;
}

static gint
EdidGetBit (gint in, gint bit)
{
        return (in & (1 << bit)) >> bit;
}

/**
 * EdidGetBits:
 **/
static gint
EdidGetBits (gint in, gint begin, gint end)
{
    gint mask = (1 << (end - begin + 1)) - 1;

    return (in >> begin) & mask;
}

/**
 * edid_decode_fraction:
 **/
static gdouble
EdidDecodeFraction (gint high, gint low)
{
        gdouble result = 0.0;
        gint i;

        high = (high << 2) | low;
        for (i = 0; i < 10; ++i)
                result += EdidGetBit (high, i) * pow (2, i - 10);
        return result;
}

static gchar *
EdidParseString (const guint8 *data)
{
        gchar *text;
        guint i;
        guint replaced = 0;

        /* this is always 13 bytes, but we can't guarantee it's null
         * terminated or not junk. */
        text = g_strndup ((const gchar *) data, 13);

        /* remove insane newline chars */
        g_strdelimit (text, "\n\r", '\0');

        /* remove spaces */
        g_strchomp (text);

        /* nothing left? */
        if (text[0] == '\0') {
                g_free (text);
                text = NULL;
                goto out;
        }

        /* ensure string is printable */
        for (i = 0; text[i] != '\0'; i++) {
                if (!g_ascii_isprint (text[i])) {
                        text[i] = '-';
                        replaced++;
                }
        }

        /* if the string is junk, ignore the string */
        if (replaced > 4) {
                g_free (text);
                text = NULL;
                goto out;
        }
out:
        return text;
}

gboolean ColorEdid::EdidParse (const guint8 *data, gsize length)
{
    gboolean ret = TRUE;
    guint i;
    guint32 serial;
    gchar *tmp;

    /* check header */
    if (length < 128) {
            USD_LOG(LOG_DEBUG,"EDID length is too small");
            ret = FALSE;
            goto out;
    }
    if (data[0] != 0x00 || data[1] != 0xff) {
            USD_LOG(LOG_DEBUG,"Failed to parse EDID header");
            ret = FALSE;
            goto out;
    }

    /* free old data */
    EdidReset ();

    /* decode the PNP ID from three 5 bit words packed into 2 bytes
     * /--08--\/--09--\
     * 7654321076543210
     * |\---/\---/\---/
     * R  C1   C2   C3 */
    pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID+0] & 0x7c) / 4) - 1;
    pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID+0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID+1] & 0xe0) / 32) - 1;
    pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID+1] & 0x1f) - 1;

    /* maybe there isn't a ASCII serial number descriptor, so use this instead */
    serial = (guint32) data[EDID_OFFSET_SERIAL+0];
    serial += (guint32) data[EDID_OFFSET_SERIAL+1] * 0x100;
    serial += (guint32) data[EDID_OFFSET_SERIAL+2] * 0x10000;
    serial += (guint32) data[EDID_OFFSET_SERIAL+3] * 0x1000000;
    if (serial > 0)
            serial_number = g_strdup_printf ("%" G_GUINT32_FORMAT, serial);

    /* get the size */
    width = data[EDID_OFFSET_SIZE+0];
    height = data[EDID_OFFSET_SIZE+1];

    /* we don't care about aspect */
    if (width == 0 || height == 0) {
            width = 0;
            height = 0;
    }

    /* get gamma */
    if (data[EDID_OFFSET_GAMMA] == 0xff) {
            gamma = 1.0f;
    } else {
            gamma = ((gfloat) data[EDID_OFFSET_GAMMA] / 100) + 1;
    }

    /* get color red */
    red->x = EdidDecodeFraction (data[0x1b], EdidGetBits (data[0x19], 6, 7));
    red->y = EdidDecodeFraction (data[0x1c], EdidGetBits (data[0x19], 4, 5));

    /* get color green */
    green->x = EdidDecodeFraction (data[0x1d], EdidGetBits (data[0x19], 2, 3));
    green->y = EdidDecodeFraction (data[0x1e], EdidGetBits (data[0x19], 0, 1));

    /* get color blue */
    blue->x = EdidDecodeFraction (data[0x1f], EdidGetBits (data[0x1a], 6, 7));
    blue->y = EdidDecodeFraction (data[0x20], EdidGetBits (data[0x1a], 4, 5));

    /* get color white */
    white->x = EdidDecodeFraction (data[0x21], EdidGetBits (data[0x1a], 2, 3));
    white->y = EdidDecodeFraction (data[0x22], EdidGetBits (data[0x1a], 0, 1));

    /* parse EDID data */
    for (i = EDID_OFFSET_DATA_BLOCKS;
         i <= EDID_OFFSET_LAST_BLOCK;
         i += 18) {
            /* ignore pixel clock data */
            if (data[i] != 0)
                    continue;
            if (data[i+2] != 0)
                    continue;

            /* any useful blocks? */
            if (data[i+3] == DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
                    tmp = EdidParseString (&data[i+5]);
                    if (tmp != NULL) {
                            g_free (monitor_name);
                            monitor_name = tmp;
                    }
            } else if (data[i+3] == DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
                    tmp = EdidParseString (&data[i+5]);
                    if (tmp != NULL) {
                            g_free (serial_number);
                            serial_number = tmp;
                    }
            } else if (data[i+3] == DESCRIPTOR_COLOR_MANAGEMENT_DATA) {
                    g_warning ("failing to parse color management data");
            } else if (data[i+3] == DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
                    tmp = EdidParseString (&data[i+5]);
                    if (tmp != NULL) {
                            g_free (eisa_id);
                            eisa_id = tmp;
                    }
            } else if (data[i+3] == DESCRIPTOR_COLOR_POINT) {
                    if (data[i+3+9] != 0xff) {
                            /* extended EDID block(1) which contains
                             * a better gamma value */
                            gamma = ((gfloat) data[i+3+9] / 100) + 1;
                    }
                    if (data[i+3+14] != 0xff) {
                            /* extended EDID block(2) which contains
                             * a better gamma value */
                            gamma = ((gfloat) data[i+3+9] / 100) + 1;
                    }
            }
    }

    /* calculate checksum */
    checksum = g_compute_checksum_for_data (G_CHECKSUM_MD5, data, length);
out:
    return ret;
}