Merge PR #29
[deb_libcec.git] / src / lib / platform / adl / adl-edid.cpp
CommitLineData
200322e5
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
16f47961 4 * libCEC(R) is Copyright (C) 2011-2013 Pulse-Eight Limited. All rights reserved.
200322e5
LOK
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
2b44051c 33#include "env.h"
200322e5
LOK
34#include "adl-edid.h"
35
36// for dlsym and friends
37#if defined(__WINDOWS__)
38#include "../windows/dlfcn-win32.h"
39#endif
40
41using namespace PLATFORM;
42
43CADLEdidParser::CADLEdidParser(void) :
44 m_bOpen(false),
45 m_handle(NULL)
46{
47 Initialise();
48}
49
50CADLEdidParser::~CADLEdidParser(void)
51{
52 CloseLibrary();
53}
54
55bool CADLEdidParser::OpenLibrary(void)
56{
57 CloseLibrary();
58
59#if !defined(__WINDOWS__)
60 m_handle = dlopen("libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
61#else
62 m_handle = LoadLibrary("atiadlxx.dll");
63 // try 32 bit
64 if (!m_handle)
65 m_handle = LoadLibrary("atiadlxy.dll");
66#endif
67
68 return m_handle != NULL;
69}
70
71void CADLEdidParser::CloseLibrary(void)
72{
73 if (LibOpen())
74 ADL_Main_Control_Destroy();
75
76 if (m_handle)
77 dlclose(m_handle);
78 m_handle = NULL;
79}
80
81void *__stdcall ADL_AllocMemory(int iSize)
82{
83 void* lpBuffer = malloc(iSize);
84 return lpBuffer;
85}
86
87void CADLEdidParser::Initialise(void)
88{
89 if (OpenLibrary())
90 {
91 // dlsym the methods we need
92 ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) dlsym(m_handle, "ADL_Main_Control_Create");
93 ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) dlsym(m_handle, "ADL_Main_Control_Destroy");
94 ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) dlsym(m_handle, "ADL_Adapter_NumberOfAdapters_Get");
95 ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET) dlsym(m_handle, "ADL_Adapter_AdapterInfo_Get");
96 ADL_Display_DisplayInfo_Get = (ADL_DISPLAY_DISPLAYINFO_GET) dlsym(m_handle, "ADL_Display_DisplayInfo_Get");
97 ADL_Display_EdidData_Get = (ADL_DISPLAY_EDIDDATA_GET) dlsym(m_handle, "ADL_Display_EdidData_Get");
98
99 // check whether they could all be resolved
100 if (ADL_Main_Control_Create &&
101 ADL_Main_Control_Destroy &&
102 ADL_Adapter_NumberOfAdapters_Get &&
103 ADL_Adapter_AdapterInfo_Get &&
104 ADL_Display_DisplayInfo_Get &&
105 ADL_Display_EdidData_Get)
106 {
107 // and try to initialise it
108 m_bOpen = (ADL_OK == ADL_Main_Control_Create(ADL_AllocMemory, 1));
109 }
110 }
111}
112
113int CADLEdidParser::GetNumAdapters(void)
114{
115 int iNumAdapters(0);
116
117 if (!LibOpen() || ADL_OK != ADL_Adapter_NumberOfAdapters_Get(&iNumAdapters))
118 iNumAdapters = 0;
119
120 return iNumAdapters;
121}
122
123LPAdapterInfo CADLEdidParser::GetAdapterInfo(int iNumAdapters)
124{
125 // validate input
126 if (iNumAdapters <= 0)
127 return NULL;
128
129 LPAdapterInfo adapterInfo = (LPAdapterInfo)malloc(sizeof(AdapterInfo) * iNumAdapters);
130 memset(adapterInfo, 0, sizeof(AdapterInfo) * iNumAdapters);
131
132 // get the info
133 ADL_Adapter_AdapterInfo_Get(adapterInfo, sizeof(AdapterInfo) * iNumAdapters);
134
135 return adapterInfo;
136}
137
138bool CADLEdidParser::GetAdapterEDID(int iAdapterIndex, int iDisplayIndex, ADLDisplayEDIDData *data)
139{
140 // validate input
141 if (iAdapterIndex < 0 || iDisplayIndex < 0)
142 return false;
143
144 memset(data, 0, sizeof(ADLDisplayEDIDData));
145 data->iSize = sizeof(ADLDisplayEDIDData);
146 data->iBlockIndex = 1;
147
148 return (ADL_Display_EdidData_Get(iAdapterIndex, iDisplayIndex, data) == ADL_OK);
149}
150
151uint16_t CADLEdidParser::GetPhysicalAddress(void)
152{
153 uint16_t iPA(0);
154
155 // get the number of adapters
156 int iNumAdapters = GetNumAdapters();
157 if (iNumAdapters <= 0)
158 return 0;
159
160 // get the adapter info
161 LPAdapterInfo adapterInfo = GetAdapterInfo(iNumAdapters);
162 if (!adapterInfo)
163 return 0;
164
165 // iterate over it
166 for (int iAdapterPtr = 0; iAdapterPtr < iNumAdapters; iAdapterPtr++)
167 {
168 int iNumDisplays(-1);
169 LPADLDisplayInfo displayInfo(NULL);
170 int iAdapterIndex = adapterInfo[iAdapterPtr].iAdapterIndex;
171
172 // get the display info
173 if (ADL_OK != ADL_Display_DisplayInfo_Get(iAdapterIndex, &iNumDisplays, &displayInfo, 0))
174 continue;
175
176 // iterate over it
177 for (int iDisplayPtr = 0; iDisplayPtr < iNumDisplays; iDisplayPtr++)
178 {
179 // check whether the display is connected
180 if ((displayInfo[iDisplayPtr].iDisplayInfoValue & ADL_DISPLAY_CONNECTED) != ADL_DISPLAY_CONNECTED)
181 continue;
182
183 int iDisplayIndex = displayInfo[iDisplayPtr].displayID.iDisplayLogicalIndex;
184
185 // try to get the EDID
186 ADLDisplayEDIDData edidData;
187 if (GetAdapterEDID(iAdapterIndex, iDisplayIndex, &edidData))
188 {
189 // try to get the PA from the EDID
190 iPA = CEDIDParser::GetPhysicalAddressFromEDID(edidData.cEDIDData, edidData.iEDIDSize);
191
192 // found it
193 if (iPA != 0)
194 break;
195 }
196 }
197
198 free(displayInfo);
199 }
200
201 free(adapterInfo);
202
203 return iPA;
204}