src/video/x11/edid-parse.c
author Sam Lantinga
Wed, 17 Apr 2013 01:38:59 -0700
changeset 7072 1d9b4b7a8ce3
parent 6992 e7c2c85bcf69
child 8922 dfb6f8611ebe
permissions -rw-r--r--
Fixed bug 1805 - Memory Leak issue in src/video/x11/edid-parse.c file

Nitz

In Function,

MonitorInfo *
decode_edid (const uchar *edid)

In this function "info" is going out of scope and leaks the storage it points to, if the first if condition get true:
if (!decode_header (edid))
return NULL;

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