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