src/video/x11/edid-parse.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 30 Oct 2016 21:01:33 +0100
changeset 10563 e3d84016cb79
parent 10030 061b1b467bd3
child 10701 62f7dd0acd13
permissions -rw-r--r--
Fixed outdated info in README.
slouken@6796
     1
/*
slouken@6796
     2
 * Copyright 2007 Red Hat, Inc.
slouken@6796
     3
 *
slouken@6796
     4
 * Permission is hereby granted, free of charge, to any person obtaining a
slouken@6796
     5
 * copy of this software and associated documentation files (the "Software"),
slouken@6796
     6
 * to deal in the Software without restriction, including without limitation
slouken@6796
     7
 * on the rights to use, copy, modify, merge, publish, distribute, sub
slouken@6796
     8
 * license, and/or sell copies of the Software, and to permit persons to whom
slouken@6796
     9
 * the Software is furnished to do so, subject to the following conditions:
slouken@6796
    10
 *
slouken@6796
    11
 * The above copyright notice and this permission notice (including the next
slouken@6796
    12
 * paragraph) shall be included in all copies or substantial portions of the
slouken@6796
    13
 * Software.
slouken@6796
    14
 *
slouken@6796
    15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
slouken@6796
    16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
slouken@6796
    17
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
slouken@6796
    18
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
slouken@6796
    19
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
slouken@6796
    20
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
slouken@6796
    21
 */
slouken@6796
    22
slouken@6796
    23
/* Author: Soren Sandmann <sandmann@redhat.com> */
icculus@10030
    24
#include "../../SDL_internal.h"
icculus@10030
    25
#include "SDL_stdinc.h"
slouken@6796
    26
slouken@6796
    27
#include "edid.h"
slouken@6796
    28
#include <stdlib.h>
slouken@6796
    29
#include <string.h>
slouken@6796
    30
#include <math.h>
slouken@6797
    31
#include <stdio.h>
slouken@6796
    32
slouken@6796
    33
#define TRUE 1
slouken@6796
    34
#define FALSE 0
slouken@6796
    35
slouken@6796
    36
static int
slouken@6796
    37
get_bit (int in, int bit)
slouken@6796
    38
{
slouken@6796
    39
    return (in & (1 << bit)) >> bit;
slouken@6796
    40
}
slouken@6796
    41
slouken@6796
    42
static int
slouken@6796
    43
get_bits (int in, int begin, int end)
slouken@6796
    44
{
slouken@6796
    45
    int mask = (1 << (end - begin + 1)) - 1;
slouken@6796
    46
    
slouken@6796
    47
    return (in >> begin) & mask;
slouken@6796
    48
}
slouken@6796
    49
slouken@6796
    50
static int
slouken@6796
    51
decode_header (const uchar *edid)
slouken@6796
    52
{
slouken@6796
    53
    if (memcmp (edid, "\x00\xff\xff\xff\xff\xff\xff\x00", 8) == 0)
slouken@6796
    54
	return TRUE;
slouken@6796
    55
    return FALSE;
slouken@6796
    56
}
slouken@6796
    57
slouken@6796
    58
static int
slouken@6796
    59
decode_vendor_and_product_identification (const uchar *edid, MonitorInfo *info)
slouken@6796
    60
{
slouken@6796
    61
    int is_model_year;
slouken@6796
    62
    
slouken@6796
    63
    /* Manufacturer Code */
slouken@6796
    64
    info->manufacturer_code[0]  = get_bits (edid[0x08], 2, 6);
slouken@6796
    65
    info->manufacturer_code[1]  = get_bits (edid[0x08], 0, 1) << 3;
slouken@6796
    66
    info->manufacturer_code[1] |= get_bits (edid[0x09], 5, 7);
slouken@6796
    67
    info->manufacturer_code[2]  = get_bits (edid[0x09], 0, 4);
slouken@6796
    68
    info->manufacturer_code[3]  = '\0';
slouken@6796
    69
    
slouken@6796
    70
    info->manufacturer_code[0] += 'A' - 1;
slouken@6796
    71
    info->manufacturer_code[1] += 'A' - 1;
slouken@6796
    72
    info->manufacturer_code[2] += 'A' - 1;
slouken@6796
    73
slouken@6796
    74
    /* Product Code */
slouken@6796
    75
    info->product_code = edid[0x0b] << 8 | edid[0x0a];
slouken@6796
    76
slouken@6796
    77
    /* Serial Number */
slouken@6796
    78
    info->serial_number =
slouken@6796
    79
	edid[0x0c] | edid[0x0d] << 8 | edid[0x0e] << 16 | edid[0x0f] << 24;
slouken@6796
    80
slouken@6796
    81
    /* Week and Year */
slouken@6796
    82
    is_model_year = FALSE;
slouken@6796
    83
    switch (edid[0x10])
slouken@6796
    84
    {
slouken@6796
    85
    case 0x00:
slouken@6796
    86
	info->production_week = -1;
slouken@6796
    87
	break;
slouken@6796
    88
slouken@6796
    89
    case 0xff:
slouken@6796
    90
	info->production_week = -1;
slouken@6796
    91
	is_model_year = TRUE;
slouken@6796
    92
	break;
slouken@6796
    93
slouken@6796
    94
    default:
slouken@6796
    95
	info->production_week = edid[0x10];
slouken@6796
    96
	break;
slouken@6796
    97
    }
slouken@6796
    98
slouken@6796
    99
    if (is_model_year)
slouken@6796
   100
    {
slouken@6796
   101
	info->production_year = -1;
slouken@6796
   102
	info->model_year = 1990 + edid[0x11];
slouken@6796
   103
    }
slouken@6796
   104
    else
slouken@6796
   105
    {
slouken@6796
   106
	info->production_year = 1990 + edid[0x11];
slouken@6796
   107
	info->model_year = -1;
slouken@6796
   108
    }
slouken@6796
   109
slouken@6796
   110
    return TRUE;
slouken@6796
   111
}
slouken@6796
   112
slouken@6796
   113
static int
slouken@6796
   114
decode_edid_version (const uchar *edid, MonitorInfo *info)
slouken@6796
   115
{
slouken@6796
   116
    info->major_version = edid[0x12];
slouken@6796
   117
    info->minor_version = edid[0x13];
slouken@6796
   118
slouken@6796
   119
    return TRUE;
slouken@6796
   120
}
slouken@6796
   121
slouken@6796
   122
static int
slouken@6796
   123
decode_display_parameters (const uchar *edid, MonitorInfo *info)
slouken@6796
   124
{
slouken@6796
   125
    /* Digital vs Analog */
slouken@6796
   126
    info->is_digital = get_bit (edid[0x14], 7);
slouken@6796
   127
slouken@6796
   128
    if (info->is_digital)
slouken@6796
   129
    {
slouken@6796
   130
	int bits;
slouken@6796
   131
	
slouken@6796
   132
	static const int bit_depth[8] =
slouken@6796
   133
	{
slouken@6796
   134
	    -1, 6, 8, 10, 12, 14, 16, -1
slouken@6796
   135
	};
slouken@6796
   136
slouken@6796
   137
	static const Interface interfaces[6] =
slouken@6796
   138
	{
slouken@6796
   139
	    UNDEFINED, DVI, HDMI_A, HDMI_B, MDDI, DISPLAY_PORT
slouken@6796
   140
	};
slouken@6796
   141
slouken@6796
   142
	bits = get_bits (edid[0x14], 4, 6);
slouken@6796
   143
	info->digital.bits_per_primary = bit_depth[bits];
slouken@6796
   144
slouken@6796
   145
	bits = get_bits (edid[0x14], 0, 3);
slouken@6796
   146
	
slouken@6796
   147
	if (bits <= 5)
slouken@6796
   148
	    info->digital.interface = interfaces[bits];
slouken@6796
   149
	else
slouken@6796
   150
	    info->digital.interface = UNDEFINED;
slouken@6796
   151
    }
slouken@6796
   152
    else
slouken@6796
   153
    {
slouken@6796
   154
	int bits = get_bits (edid[0x14], 5, 6);
slouken@6796
   155
	
slouken@6796
   156
	static const double levels[][3] =
slouken@6796
   157
	{
slouken@6796
   158
	    { 0.7,   0.3,    1.0 },
slouken@6796
   159
	    { 0.714, 0.286,  1.0 },
slouken@6796
   160
	    { 1.0,   0.4,    1.4 },
slouken@6796
   161
	    { 0.7,   0.0,    0.7 },
slouken@6796
   162
	};
slouken@6796
   163
slouken@6796
   164
	info->analog.video_signal_level = levels[bits][0];
slouken@6796
   165
	info->analog.sync_signal_level = levels[bits][1];
slouken@6796
   166
	info->analog.total_signal_level = levels[bits][2];
slouken@6796
   167
slouken@6796
   168
	info->analog.blank_to_black = get_bit (edid[0x14], 4);
slouken@6796
   169
slouken@6796
   170
	info->analog.separate_hv_sync = get_bit (edid[0x14], 3);
slouken@6796
   171
	info->analog.composite_sync_on_h = get_bit (edid[0x14], 2);
slouken@6796
   172
	info->analog.composite_sync_on_green = get_bit (edid[0x14], 1);
slouken@6796
   173
slouken@6796
   174
	info->analog.serration_on_vsync = get_bit (edid[0x14], 0);
slouken@6796
   175
    }
slouken@6796
   176
slouken@6796
   177
    /* Screen Size / Aspect Ratio */
slouken@6796
   178
    if (edid[0x15] == 0 && edid[0x16] == 0)
slouken@6796
   179
    {
slouken@6796
   180
	info->width_mm = -1;
slouken@6796
   181
	info->height_mm = -1;
slouken@6796
   182
	info->aspect_ratio = -1.0;
slouken@6796
   183
    }
slouken@6796
   184
    else if (edid[0x16] == 0)
slouken@6796
   185
    {
slouken@6796
   186
	info->width_mm = -1;
slouken@6796
   187
	info->height_mm = -1; 
slouken@6796
   188
	info->aspect_ratio = 100.0 / (edid[0x15] + 99);
slouken@6796
   189
    }
slouken@6796
   190
    else if (edid[0x15] == 0)
slouken@6796
   191
    {
slouken@6796
   192
	info->width_mm = -1;
slouken@6796
   193
	info->height_mm = -1;
slouken@6796
   194
	info->aspect_ratio = 100.0 / (edid[0x16] + 99);
slouken@6796
   195
	info->aspect_ratio = 1/info->aspect_ratio; /* portrait */
slouken@6796
   196
    }
slouken@6796
   197
    else
slouken@6796
   198
    {
slouken@6796
   199
	info->width_mm = 10 * edid[0x15];
slouken@6796
   200
	info->height_mm = 10 * edid[0x16];
slouken@6796
   201
    }
slouken@6796
   202
slouken@6796
   203
    /* Gamma */
slouken@6796
   204
    if (edid[0x17] == 0xFF)
slouken@6796
   205
	info->gamma = -1.0;
slouken@6796
   206
    else
slouken@6796
   207
	info->gamma = (edid[0x17] + 100.0) / 100.0;
slouken@6796
   208
slouken@6796
   209
    /* Features */
slouken@6796
   210
    info->standby = get_bit (edid[0x18], 7);
slouken@6796
   211
    info->suspend = get_bit (edid[0x18], 6);
slouken@6796
   212
    info->active_off = get_bit (edid[0x18], 5);
slouken@6796
   213
slouken@6796
   214
    if (info->is_digital)
slouken@6796
   215
    {
slouken@6796
   216
	info->digital.rgb444 = TRUE;
slouken@6796
   217
	if (get_bit (edid[0x18], 3))
slouken@6796
   218
	    info->digital.ycrcb444 = 1;
slouken@6796
   219
	if (get_bit (edid[0x18], 4))
slouken@6796
   220
	    info->digital.ycrcb422 = 1;
slouken@6796
   221
    }
slouken@6796
   222
    else
slouken@6796
   223
    {
slouken@6796
   224
	int bits = get_bits (edid[0x18], 3, 4);
slouken@6796
   225
	ColorType color_type[4] =
slouken@6796
   226
	{
slouken@6796
   227
	    MONOCHROME, RGB, OTHER_COLOR, UNDEFINED_COLOR
slouken@6796
   228
	};
slouken@6796
   229
slouken@6796
   230
	info->analog.color_type = color_type[bits];
slouken@6796
   231
    }
slouken@6796
   232
slouken@6796
   233
    info->srgb_is_standard = get_bit (edid[0x18], 2);
slouken@6796
   234
slouken@6796
   235
    /* In 1.3 this is called "has preferred timing" */
slouken@6796
   236
    info->preferred_timing_includes_native = get_bit (edid[0x18], 1);
slouken@6796
   237
slouken@6796
   238
    /* FIXME: In 1.3 this indicates whether the monitor accepts GTF */
slouken@6796
   239
    info->continuous_frequency = get_bit (edid[0x18], 0);
slouken@6796
   240
    return TRUE;
slouken@6796
   241
}
slouken@6796
   242
slouken@6796
   243
static double
slouken@6796
   244
decode_fraction (int high, int low)
slouken@6796
   245
{
slouken@6796
   246
    double result = 0.0;
slouken@6796
   247
    int i;
slouken@6796
   248
slouken@6796
   249
    high = (high << 2) | low;
slouken@6796
   250
slouken@6796
   251
    for (i = 0; i < 10; ++i)
icculus@10030
   252
	result += get_bit (high, i) * SDL_pow (2, i - 10);
slouken@6796
   253
slouken@6796
   254
    return result;
slouken@6796
   255
}
slouken@6796
   256
slouken@6796
   257
static int
slouken@6796
   258
decode_color_characteristics (const uchar *edid, MonitorInfo *info)
slouken@6796
   259
{
slouken@6796
   260
    info->red_x = decode_fraction (edid[0x1b], get_bits (edid[0x19], 6, 7));
slouken@6796
   261
    info->red_y = decode_fraction (edid[0x1c], get_bits (edid[0x19], 5, 4));
slouken@6796
   262
    info->green_x = decode_fraction (edid[0x1d], get_bits (edid[0x19], 2, 3));
slouken@6796
   263
    info->green_y = decode_fraction (edid[0x1e], get_bits (edid[0x19], 0, 1));
slouken@6796
   264
    info->blue_x = decode_fraction (edid[0x1f], get_bits (edid[0x1a], 6, 7));
slouken@6796
   265
    info->blue_y = decode_fraction (edid[0x20], get_bits (edid[0x1a], 4, 5));
slouken@6796
   266
    info->white_x = decode_fraction (edid[0x21], get_bits (edid[0x1a], 2, 3));
slouken@6796
   267
    info->white_y = decode_fraction (edid[0x22], get_bits (edid[0x1a], 0, 1));
slouken@6796
   268
slouken@6796
   269
    return TRUE;
slouken@6796
   270
}
slouken@6796
   271
slouken@6796
   272
static int
slouken@6796
   273
decode_established_timings (const uchar *edid, MonitorInfo *info)
slouken@6796
   274
{
slouken@6796
   275
    static const Timing established[][8] = 
slouken@6796
   276
    {
slouken@6796
   277
	{
slouken@6796
   278
	    { 800, 600, 60 },
slouken@6796
   279
	    { 800, 600, 56 },
slouken@6796
   280
	    { 640, 480, 75 },
slouken@6796
   281
	    { 640, 480, 72 },
slouken@6796
   282
	    { 640, 480, 67 },
slouken@6796
   283
	    { 640, 480, 60 },
slouken@6796
   284
	    { 720, 400, 88 },
slouken@6796
   285
	    { 720, 400, 70 }
slouken@6796
   286
	},
slouken@6796
   287
	{
slouken@6796
   288
	    { 1280, 1024, 75 },
slouken@6796
   289
	    { 1024, 768, 75 },
slouken@6796
   290
	    { 1024, 768, 70 },
slouken@6796
   291
	    { 1024, 768, 60 },
slouken@6796
   292
	    { 1024, 768, 87 },
slouken@6796
   293
	    { 832, 624, 75 },
slouken@6796
   294
	    { 800, 600, 75 },
slouken@6796
   295
	    { 800, 600, 72 }
slouken@6796
   296
	},
slouken@6796
   297
	{
slouken@6796
   298
	    { 0, 0, 0 },
slouken@6796
   299
	    { 0, 0, 0 },
slouken@6796
   300
	    { 0, 0, 0 },
slouken@6796
   301
	    { 0, 0, 0 },
slouken@6796
   302
	    { 0, 0, 0 },
slouken@6796
   303
	    { 0, 0, 0 },
slouken@6796
   304
	    { 0, 0, 0 },
slouken@6796
   305
	    { 1152, 870, 75 }
slouken@6796
   306
	},
slouken@6796
   307
    };
slouken@6796
   308
slouken@6796
   309
    int i, j, idx;
slouken@6796
   310
slouken@6796
   311
    idx = 0;
slouken@6796
   312
    for (i = 0; i < 3; ++i)
slouken@6796
   313
    {
slouken@6796
   314
	for (j = 0; j < 8; ++j)
slouken@6796
   315
	{
slouken@6796
   316
	    int byte = edid[0x23 + i];
slouken@6796
   317
slouken@6796
   318
	    if (get_bit (byte, j) && established[i][j].frequency != 0)
slouken@6796
   319
		info->established[idx++] = established[i][j];
slouken@6796
   320
	}
slouken@6796
   321
    }
slouken@6796
   322
    return TRUE;
slouken@6796
   323
}
slouken@6796
   324
slouken@6796
   325
static int
slouken@6796
   326
decode_standard_timings (const uchar *edid, MonitorInfo *info)
slouken@6796
   327
{
slouken@6796
   328
    int i;
slouken@6796
   329
    
slouken@6796
   330
    for (i = 0; i < 8; i++)
slouken@6796
   331
    {
slouken@6796
   332
	int first = edid[0x26 + 2 * i];
slouken@6796
   333
	int second = edid[0x27 + 2 * i];
slouken@6796
   334
slouken@6796
   335
	if (first != 0x01 && second != 0x01)
slouken@6796
   336
	{
slouken@6796
   337
	    int w = 8 * (first + 31);
icculus@6992
   338
	    int h = 0;
slouken@6796
   339
slouken@6796
   340
	    switch (get_bits (second, 6, 7))
slouken@6796
   341
	    {
slouken@6796
   342
	    case 0x00: h = (w / 16) * 10; break;
slouken@6796
   343
	    case 0x01: h = (w / 4) * 3; break;
slouken@6796
   344
	    case 0x02: h = (w / 5) * 4; break;
slouken@6796
   345
	    case 0x03: h = (w / 16) * 9; break;
slouken@6796
   346
	    }
slouken@6796
   347
slouken@6796
   348
	    info->standard[i].width = w;
slouken@6796
   349
	    info->standard[i].height = h;
slouken@6796
   350
	    info->standard[i].frequency = get_bits (second, 0, 5) + 60;
slouken@6796
   351
	}
slouken@6796
   352
    }
slouken@6796
   353
    
slouken@6796
   354
    return TRUE;
slouken@6796
   355
}
slouken@6796
   356
slouken@6796
   357
static void
slouken@6796
   358
decode_lf_string (const uchar *s, int n_chars, char *result)
slouken@6796
   359
{
slouken@6796
   360
    int i;
slouken@6796
   361
    for (i = 0; i < n_chars; ++i)
slouken@6796
   362
    {
slouken@6796
   363
	if (s[i] == 0x0a)
slouken@6796
   364
	{
slouken@6796
   365
	    *result++ = '\0';
slouken@6796
   366
	    break;
slouken@6796
   367
	}
slouken@6796
   368
	else if (s[i] == 0x00)
slouken@6796
   369
	{
slouken@6796
   370
	    /* Convert embedded 0's to spaces */
slouken@6796
   371
	    *result++ = ' ';
slouken@6796
   372
	}
slouken@6796
   373
	else
slouken@6796
   374
	{
slouken@6796
   375
	    *result++ = s[i];
slouken@6796
   376
	}
slouken@6796
   377
    }
slouken@6796
   378
}
slouken@6796
   379
slouken@6796
   380
static void
slouken@6796
   381
decode_display_descriptor (const uchar *desc,
slouken@6796
   382
			   MonitorInfo *info)
slouken@6796
   383
{
slouken@6796
   384
    switch (desc[0x03])
slouken@6796
   385
    {
slouken@6796
   386
    case 0xFC:
slouken@6796
   387
	decode_lf_string (desc + 5, 13, info->dsc_product_name);
slouken@6796
   388
	break;
slouken@6796
   389
    case 0xFF:
slouken@6796
   390
	decode_lf_string (desc + 5, 13, info->dsc_serial_number);
slouken@6796
   391
	break;
slouken@6796
   392
    case 0xFE:
slouken@6796
   393
	decode_lf_string (desc + 5, 13, info->dsc_string);
slouken@6796
   394
	break;
slouken@6796
   395
    case 0xFD:
slouken@6796
   396
	/* Range Limits */
slouken@6796
   397
	break;
slouken@6796
   398
    case 0xFB:
slouken@6796
   399
	/* Color Point */
slouken@6796
   400
	break;
slouken@6796
   401
    case 0xFA:
slouken@6796
   402
	/* Timing Identifications */
slouken@6796
   403
	break;
slouken@6796
   404
    case 0xF9:
slouken@6796
   405
	/* Color Management */
slouken@6796
   406
	break;
slouken@6796
   407
    case 0xF8:
slouken@6796
   408
	/* Timing Codes */
slouken@6796
   409
	break;
slouken@6796
   410
    case 0xF7:
slouken@6796
   411
	/* Established Timings */
slouken@6796
   412
	break;
slouken@6796
   413
    case 0x10:
slouken@6796
   414
	break;
slouken@6796
   415
    }
slouken@6796
   416
}
slouken@6796
   417
slouken@6796
   418
static void
slouken@6796
   419
decode_detailed_timing (const uchar *timing,
slouken@6796
   420
			DetailedTiming *detailed)
slouken@6796
   421
{
slouken@6796
   422
    int bits;
slouken@6796
   423
    StereoType stereo[] =
slouken@6796
   424
    {
slouken@6796
   425
	NO_STEREO, NO_STEREO, FIELD_RIGHT, FIELD_LEFT,
slouken@6796
   426
	TWO_WAY_RIGHT_ON_EVEN, TWO_WAY_LEFT_ON_EVEN,
slouken@6796
   427
	FOUR_WAY_INTERLEAVED, SIDE_BY_SIDE
slouken@6796
   428
    };
slouken@6796
   429
    
slouken@6796
   430
    detailed->pixel_clock = (timing[0x00] | timing[0x01] << 8) * 10000;
slouken@6796
   431
    detailed->h_addr = timing[0x02] | ((timing[0x04] & 0xf0) << 4);
slouken@6796
   432
    detailed->h_blank = timing[0x03] | ((timing[0x04] & 0x0f) << 8);
slouken@6796
   433
    detailed->v_addr = timing[0x05] | ((timing[0x07] & 0xf0) << 4);
slouken@6796
   434
    detailed->v_blank = timing[0x06] | ((timing[0x07] & 0x0f) << 8);
slouken@6796
   435
    detailed->h_front_porch = timing[0x08] | get_bits (timing[0x0b], 6, 7) << 8;
slouken@6796
   436
    detailed->h_sync = timing[0x09] | get_bits (timing[0x0b], 4, 5) << 8;
slouken@6796
   437
    detailed->v_front_porch =
slouken@6796
   438
	get_bits (timing[0x0a], 4, 7) | get_bits (timing[0x0b], 2, 3) << 4;
slouken@6796
   439
    detailed->v_sync =
slouken@6796
   440
	get_bits (timing[0x0a], 0, 3) | get_bits (timing[0x0b], 0, 1) << 4;
slouken@6796
   441
    detailed->width_mm =  timing[0x0c] | get_bits (timing[0x0e], 4, 7) << 8;
slouken@6796
   442
    detailed->height_mm = timing[0x0d] | get_bits (timing[0x0e], 0, 3) << 8;
slouken@6796
   443
    detailed->right_border = timing[0x0f];
slouken@6796
   444
    detailed->top_border = timing[0x10];
slouken@6796
   445
slouken@6796
   446
    detailed->interlaced = get_bit (timing[0x11], 7);
slouken@6796
   447
slouken@6796
   448
    /* Stereo */
slouken@6796
   449
    bits = get_bits (timing[0x11], 5, 6) << 1 | get_bit (timing[0x11], 0);
slouken@6796
   450
    detailed->stereo = stereo[bits];
slouken@6796
   451
slouken@6796
   452
    /* Sync */
slouken@6796
   453
    bits = timing[0x11];
slouken@6796
   454
slouken@6796
   455
    detailed->digital_sync = get_bit (bits, 4);
slouken@6796
   456
    if (detailed->digital_sync)
slouken@6796
   457
    {
slouken@6796
   458
	detailed->digital.composite = !get_bit (bits, 3);
slouken@6796
   459
slouken@6796
   460
	if (detailed->digital.composite)
slouken@6796
   461
	{
slouken@6796
   462
	    detailed->digital.serrations = get_bit (bits, 2);
slouken@6796
   463
	    detailed->digital.negative_vsync = FALSE;
slouken@6796
   464
	}
slouken@6796
   465
	else
slouken@6796
   466
	{
slouken@6796
   467
	    detailed->digital.serrations = FALSE;
slouken@6796
   468
	    detailed->digital.negative_vsync = !get_bit (bits, 2);
slouken@6796
   469
	}
slouken@6796
   470
slouken@6796
   471
	detailed->digital.negative_hsync = !get_bit (bits, 0);
slouken@6796
   472
    }
slouken@6796
   473
    else
slouken@6796
   474
    {
slouken@6796
   475
	detailed->analog.bipolar = get_bit (bits, 3);
slouken@6796
   476
	detailed->analog.serrations = get_bit (bits, 2);
slouken@6796
   477
	detailed->analog.sync_on_green = !get_bit (bits, 1);
slouken@6796
   478
    }
slouken@6796
   479
}
slouken@6796
   480
slouken@6796
   481
static int
slouken@6796
   482
decode_descriptors (const uchar *edid, MonitorInfo *info)
slouken@6796
   483
{
slouken@6796
   484
    int i;
slouken@6796
   485
    int timing_idx;
slouken@6796
   486
    
slouken@6796
   487
    timing_idx = 0;
slouken@6796
   488
    
slouken@6796
   489
    for (i = 0; i < 4; ++i)
slouken@6796
   490
    {
slouken@6796
   491
	int index = 0x36 + i * 18;
slouken@6796
   492
slouken@6796
   493
	if (edid[index + 0] == 0x00 && edid[index + 1] == 0x00)
slouken@6796
   494
	{
slouken@6796
   495
	    decode_display_descriptor (edid + index, info);
slouken@6796
   496
	}
slouken@6796
   497
	else
slouken@6796
   498
	{
slouken@6796
   499
	    decode_detailed_timing (
slouken@6796
   500
		edid + index, &(info->detailed_timings[timing_idx++]));
slouken@6796
   501
	}
slouken@6796
   502
    }
slouken@6796
   503
slouken@6796
   504
    info->n_detailed_timings = timing_idx;
slouken@6796
   505
slouken@6796
   506
    return TRUE;
slouken@6796
   507
}
slouken@6796
   508
slouken@6796
   509
static void
slouken@6796
   510
decode_check_sum (const uchar *edid,
slouken@6796
   511
		  MonitorInfo *info)
slouken@6796
   512
{
slouken@6796
   513
    int i;
slouken@6796
   514
    uchar check = 0;
slouken@6796
   515
slouken@6796
   516
    for (i = 0; i < 128; ++i)
slouken@6796
   517
	check += edid[i];
slouken@6796
   518
slouken@6796
   519
    info->checksum = check;
slouken@6796
   520
}
slouken@6796
   521
slouken@6796
   522
MonitorInfo *
slouken@6796
   523
decode_edid (const uchar *edid)
slouken@6796
   524
{
slouken@6796
   525
    MonitorInfo *info = calloc (1, sizeof (MonitorInfo));
slouken@6796
   526
slouken@6796
   527
    decode_check_sum (edid, info);
slouken@6796
   528
    
slouken@7072
   529
    if (!decode_header (edid) ||
slouken@7072
   530
        !decode_vendor_and_product_identification (edid, info) ||
slouken@7072
   531
        !decode_edid_version (edid, info) ||
slouken@7072
   532
        !decode_display_parameters (edid, info) ||
slouken@7072
   533
        !decode_color_characteristics (edid, info) ||
slouken@7072
   534
        !decode_established_timings (edid, info) ||
slouken@7072
   535
        !decode_standard_timings (edid, info) ||
slouken@7072
   536
        !decode_descriptors (edid, info)) {
slouken@7072
   537
        free(info);
slouken@6796
   538
	return NULL;
slouken@7072
   539
    }
slouken@6796
   540
    
slouken@6796
   541
    return info;
slouken@6796
   542
}
slouken@6796
   543
slouken@6796
   544
static const char *
slouken@6796
   545
yesno (int v)
slouken@6796
   546
{
slouken@6796
   547
    return v? "yes" : "no";
slouken@6796
   548
}
slouken@6796
   549
slouken@6796
   550
void
slouken@6796
   551
dump_monitor_info (MonitorInfo *info)
slouken@6796
   552
{
slouken@6796
   553
    int i;
slouken@6796
   554
    
slouken@6796
   555
    printf ("Checksum: %d (%s)\n",
slouken@6796
   556
	    info->checksum, info->checksum? "incorrect" : "correct");
slouken@6796
   557
    printf ("Manufacturer Code: %s\n", info->manufacturer_code);
slouken@6796
   558
    printf ("Product Code: 0x%x\n", info->product_code);
slouken@6796
   559
    printf ("Serial Number: %u\n", info->serial_number);
slouken@6796
   560
    
slouken@6796
   561
    if (info->production_week != -1)
slouken@6796
   562
	printf ("Production Week: %d\n", info->production_week);
slouken@6796
   563
    else
slouken@6796
   564
	printf ("Production Week: unspecified\n");
slouken@6796
   565
    
slouken@6796
   566
    if (info->production_year != -1)
slouken@6796
   567
	printf ("Production Year: %d\n", info->production_year);
slouken@6796
   568
    else
slouken@6796
   569
	printf ("Production Year: unspecified\n");
slouken@6796
   570
    
slouken@6796
   571
    if (info->model_year != -1)
slouken@6796
   572
	printf ("Model Year: %d\n", info->model_year);
slouken@6796
   573
    else
slouken@6796
   574
	printf ("Model Year: unspecified\n");
slouken@6796
   575
    
slouken@6796
   576
    printf ("EDID revision: %d.%d\n", info->major_version, info->minor_version);
slouken@6796
   577
    
slouken@6796
   578
    printf ("Display is %s\n", info->is_digital? "digital" : "analog");
slouken@6796
   579
    if (info->is_digital)
slouken@6796
   580
    {
slouken@6796
   581
	const char *interface;
slouken@6796
   582
	if (info->digital.bits_per_primary != -1)
slouken@6796
   583
	    printf ("Bits Per Primary: %d\n", info->digital.bits_per_primary);
slouken@6796
   584
	else
slouken@6796
   585
	    printf ("Bits Per Primary: undefined\n");
slouken@6796
   586
	
slouken@6796
   587
	switch (info->digital.interface)
slouken@6796
   588
	{
slouken@6796
   589
	case DVI: interface = "DVI"; break;
slouken@6796
   590
	case HDMI_A: interface = "HDMI-a"; break;
slouken@6796
   591
	case HDMI_B: interface = "HDMI-b"; break;
slouken@6796
   592
	case MDDI: interface = "MDDI"; break;
slouken@6796
   593
	case DISPLAY_PORT: interface = "DisplayPort"; break;
slouken@6796
   594
	case UNDEFINED: interface = "undefined"; break;
slouken@6851
   595
	default: interface = "unknown"; break;
slouken@6796
   596
	}
slouken@6796
   597
	printf ("Interface: %s\n", interface);
slouken@6796
   598
	
slouken@6796
   599
	printf ("RGB 4:4:4: %s\n", yesno (info->digital.rgb444));
slouken@6796
   600
	printf ("YCrCb 4:4:4: %s\n", yesno (info->digital.ycrcb444));
slouken@6796
   601
	printf ("YCrCb 4:2:2: %s\n", yesno (info->digital.ycrcb422));
slouken@6796
   602
    }
slouken@6796
   603
    else
slouken@6796
   604
    {
slouken@8922
   605
       const char *s;
slouken@6796
   606
	printf ("Video Signal Level: %f\n", info->analog.video_signal_level);
slouken@6796
   607
	printf ("Sync Signal Level: %f\n", info->analog.sync_signal_level);
slouken@6796
   608
	printf ("Total Signal Level: %f\n", info->analog.total_signal_level);
slouken@6796
   609
	
slouken@6796
   610
	printf ("Blank to Black: %s\n",
slouken@6796
   611
		yesno (info->analog.blank_to_black));
slouken@6796
   612
	printf ("Separate HV Sync: %s\n",
slouken@6796
   613
		yesno (info->analog.separate_hv_sync));
slouken@6796
   614
	printf ("Composite Sync on H: %s\n",
slouken@6796
   615
		yesno (info->analog.composite_sync_on_h));
slouken@6796
   616
	printf ("Serration on VSync: %s\n",
slouken@6796
   617
		yesno (info->analog.serration_on_vsync));
slouken@6796
   618
	
slouken@6796
   619
	switch (info->analog.color_type)
slouken@6796
   620
	{
slouken@6796
   621
	case UNDEFINED_COLOR: s = "undefined"; break;
slouken@6796
   622
	case MONOCHROME: s = "monochrome"; break;
slouken@6796
   623
	case RGB: s = "rgb"; break;
slouken@6796
   624
	case OTHER_COLOR: s = "other color"; break;
slouken@6851
   625
	default: s = "unknown"; break;
slouken@6796
   626
	};
slouken@6796
   627
	
slouken@6796
   628
	printf ("Color: %s\n", s);
slouken@6796
   629
    }
slouken@6796
   630
    
slouken@6796
   631
    if (info->width_mm == -1)
slouken@6796
   632
	printf ("Width: undefined\n");
slouken@6796
   633
    else
slouken@6796
   634
	printf ("Width: %d mm\n", info->width_mm);
slouken@6796
   635
    
slouken@6796
   636
    if (info->height_mm == -1)
slouken@6796
   637
	printf ("Height: undefined\n");
slouken@6796
   638
    else
slouken@6796
   639
	printf ("Height: %d mm\n", info->height_mm);
slouken@6796
   640
    
slouken@6796
   641
    if (info->aspect_ratio > 0)
slouken@6796
   642
	printf ("Aspect Ratio: %f\n", info->aspect_ratio);
slouken@6796
   643
    else
slouken@6796
   644
	printf ("Aspect Ratio: undefined\n");
slouken@6796
   645
    
slouken@6796
   646
    if (info->gamma >= 0)
slouken@6796
   647
	printf ("Gamma: %f\n", info->gamma);
slouken@6796
   648
    else
slouken@6796
   649
	printf ("Gamma: undefined\n");
slouken@6796
   650
    
slouken@6796
   651
    printf ("Standby: %s\n", yesno (info->standby));
slouken@6796
   652
    printf ("Suspend: %s\n", yesno (info->suspend));
slouken@6796
   653
    printf ("Active Off: %s\n", yesno (info->active_off));
slouken@6796
   654
    
slouken@6796
   655
    printf ("SRGB is Standard: %s\n", yesno (info->srgb_is_standard));
slouken@6796
   656
    printf ("Preferred Timing Includes Native: %s\n",
slouken@6796
   657
	    yesno (info->preferred_timing_includes_native));
slouken@6796
   658
    printf ("Continuous Frequency: %s\n", yesno (info->continuous_frequency));
slouken@6796
   659
    
slouken@6796
   660
    printf ("Red X: %f\n", info->red_x);
slouken@6796
   661
    printf ("Red Y: %f\n", info->red_y);
slouken@6796
   662
    printf ("Green X: %f\n", info->green_x);
slouken@6796
   663
    printf ("Green Y: %f\n", info->green_y);
slouken@6796
   664
    printf ("Blue X: %f\n", info->blue_x);
slouken@6796
   665
    printf ("Blue Y: %f\n", info->blue_y);
slouken@6796
   666
    printf ("White X: %f\n", info->white_x);
slouken@6796
   667
    printf ("White Y: %f\n", info->white_y);
slouken@6796
   668
    
slouken@6796
   669
    printf ("Established Timings:\n");
slouken@6796
   670
    
slouken@6796
   671
    for (i = 0; i < 24; ++i)
slouken@6796
   672
    {
slouken@6796
   673
	Timing *timing = &(info->established[i]);
slouken@6796
   674
	
slouken@6796
   675
	if (timing->frequency == 0)
slouken@6796
   676
	    break;
slouken@6796
   677
	
slouken@6796
   678
	printf ("  %d x %d @ %d Hz\n",
slouken@6796
   679
		timing->width, timing->height, timing->frequency);
slouken@6796
   680
	
slouken@6796
   681
    }
slouken@6796
   682
    
slouken@6796
   683
    printf ("Standard Timings:\n");
slouken@6796
   684
    for (i = 0; i < 8; ++i)
slouken@6796
   685
    {
slouken@6796
   686
	Timing *timing = &(info->standard[i]);
slouken@6796
   687
	
slouken@6796
   688
	if (timing->frequency == 0)
slouken@6796
   689
	    break;
slouken@6796
   690
	
slouken@6796
   691
	printf ("  %d x %d @ %d Hz\n",
slouken@6796
   692
		timing->width, timing->height, timing->frequency);
slouken@6796
   693
    }
slouken@6796
   694
    
slouken@6796
   695
    for (i = 0; i < info->n_detailed_timings; ++i)
slouken@6796
   696
    {
slouken@6796
   697
	DetailedTiming *timing = &(info->detailed_timings[i]);
slouken@6796
   698
	const char *s;
slouken@6796
   699
	
slouken@6796
   700
	printf ("Timing%s: \n",
slouken@6796
   701
		(i == 0 && info->preferred_timing_includes_native)?
slouken@6796
   702
		" (Preferred)" : "");
slouken@6796
   703
	printf ("  Pixel Clock: %d\n", timing->pixel_clock);
slouken@6796
   704
	printf ("  H Addressable: %d\n", timing->h_addr);
slouken@6796
   705
	printf ("  H Blank: %d\n", timing->h_blank);
slouken@6796
   706
	printf ("  H Front Porch: %d\n", timing->h_front_porch);
slouken@6796
   707
	printf ("  H Sync: %d\n", timing->h_sync);
slouken@6796
   708
	printf ("  V Addressable: %d\n", timing->v_addr);
slouken@6796
   709
	printf ("  V Blank: %d\n", timing->v_blank);
slouken@6796
   710
	printf ("  V Front Porch: %d\n", timing->v_front_porch);
slouken@6796
   711
	printf ("  V Sync: %d\n", timing->v_sync);
slouken@6796
   712
	printf ("  Width: %d mm\n", timing->width_mm);
slouken@6796
   713
	printf ("  Height: %d mm\n", timing->height_mm);
slouken@6796
   714
	printf ("  Right Border: %d\n", timing->right_border);
slouken@6796
   715
	printf ("  Top Border: %d\n", timing->top_border);
slouken@6796
   716
	switch (timing->stereo)
slouken@6796
   717
	{
slouken@6797
   718
	default:
slouken@6796
   719
	case NO_STEREO:   s = "No Stereo"; break;
slouken@6796
   720
	case FIELD_RIGHT: s = "Field Sequential, Right on Sync"; break;
slouken@6796
   721
	case FIELD_LEFT:  s = "Field Sequential, Left on Sync"; break;
slouken@6796
   722
	case TWO_WAY_RIGHT_ON_EVEN: s = "Two-way, Right on Even"; break;
slouken@6796
   723
	case TWO_WAY_LEFT_ON_EVEN:  s = "Two-way, Left on Even"; break;
slouken@6796
   724
	case FOUR_WAY_INTERLEAVED:  s = "Four-way Interleaved"; break;
slouken@6796
   725
	case SIDE_BY_SIDE:          s = "Side-by-Side"; break;
slouken@6796
   726
	}
slouken@6796
   727
	printf ("  Stereo: %s\n", s);
slouken@6796
   728
	
slouken@6796
   729
	if (timing->digital_sync)
slouken@6796
   730
	{
slouken@6796
   731
	    printf ("  Digital Sync:\n");
slouken@6796
   732
	    printf ("    composite: %s\n", yesno (timing->digital.composite));
slouken@6796
   733
	    printf ("    serrations: %s\n", yesno (timing->digital.serrations));
slouken@6796
   734
	    printf ("    negative vsync: %s\n",
slouken@6796
   735
		    yesno (timing->digital.negative_vsync));
slouken@6796
   736
	    printf ("    negative hsync: %s\n",
slouken@6796
   737
		    yesno (timing->digital.negative_hsync));
slouken@6796
   738
	}
slouken@6796
   739
	else
slouken@6796
   740
	{
slouken@6796
   741
	    printf ("  Analog Sync:\n");
slouken@6796
   742
	    printf ("    bipolar: %s\n", yesno (timing->analog.bipolar));
slouken@6796
   743
	    printf ("    serrations: %s\n", yesno (timing->analog.serrations));
slouken@6796
   744
	    printf ("    sync on green: %s\n", yesno (
slouken@6796
   745
			timing->analog.sync_on_green));
slouken@6796
   746
	}
slouken@6796
   747
    }
slouken@6796
   748
    
slouken@6796
   749
    printf ("Detailed Product information:\n");
slouken@6796
   750
    printf ("  Product Name: %s\n", info->dsc_product_name);
slouken@6796
   751
    printf ("  Serial Number: %s\n", info->dsc_serial_number);
slouken@6796
   752
    printf ("  Unspecified String: %s\n", info->dsc_string);
slouken@6796
   753
}
slouken@6796
   754