bump version numbers to 1.7.0 and updated changelog
[deb_libcec.git] / src / lib / platform / adl / adl-edid.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
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
33 #include "adl-edid.h"
34
35 // for dlsym and friends
36 #if defined(__WINDOWS__)
37 #include "../windows/dlfcn-win32.h"
38 #endif
39
40 using namespace PLATFORM;
41
42 CADLEdidParser::CADLEdidParser(void) :
43 m_bOpen(false),
44 m_handle(NULL)
45 {
46 Initialise();
47 }
48
49 CADLEdidParser::~CADLEdidParser(void)
50 {
51 CloseLibrary();
52 }
53
54 bool CADLEdidParser::OpenLibrary(void)
55 {
56 CloseLibrary();
57
58 #if !defined(__WINDOWS__)
59 m_handle = dlopen("libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
60 #else
61 m_handle = LoadLibrary("atiadlxx.dll");
62 // try 32 bit
63 if (!m_handle)
64 m_handle = LoadLibrary("atiadlxy.dll");
65 #endif
66
67 return m_handle != NULL;
68 }
69
70 void CADLEdidParser::CloseLibrary(void)
71 {
72 if (LibOpen())
73 ADL_Main_Control_Destroy();
74
75 if (m_handle)
76 dlclose(m_handle);
77 m_handle = NULL;
78 }
79
80 void *__stdcall ADL_AllocMemory(int iSize)
81 {
82 void* lpBuffer = malloc(iSize);
83 return lpBuffer;
84 }
85
86 void CADLEdidParser::Initialise(void)
87 {
88 if (OpenLibrary())
89 {
90 // dlsym the methods we need
91 ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE) dlsym(m_handle, "ADL_Main_Control_Create");
92 ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY) dlsym(m_handle, "ADL_Main_Control_Destroy");
93 ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET) dlsym(m_handle, "ADL_Adapter_NumberOfAdapters_Get");
94 ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET) dlsym(m_handle, "ADL_Adapter_AdapterInfo_Get");
95 ADL_Display_DisplayInfo_Get = (ADL_DISPLAY_DISPLAYINFO_GET) dlsym(m_handle, "ADL_Display_DisplayInfo_Get");
96 ADL_Display_EdidData_Get = (ADL_DISPLAY_EDIDDATA_GET) dlsym(m_handle, "ADL_Display_EdidData_Get");
97
98 // check whether they could all be resolved
99 if (ADL_Main_Control_Create &&
100 ADL_Main_Control_Destroy &&
101 ADL_Adapter_NumberOfAdapters_Get &&
102 ADL_Adapter_AdapterInfo_Get &&
103 ADL_Display_DisplayInfo_Get &&
104 ADL_Display_EdidData_Get)
105 {
106 // and try to initialise it
107 m_bOpen = (ADL_OK == ADL_Main_Control_Create(ADL_AllocMemory, 1));
108 }
109 }
110 }
111
112 int CADLEdidParser::GetNumAdapters(void)
113 {
114 int iNumAdapters(0);
115
116 if (!LibOpen() || ADL_OK != ADL_Adapter_NumberOfAdapters_Get(&iNumAdapters))
117 iNumAdapters = 0;
118
119 return iNumAdapters;
120 }
121
122 LPAdapterInfo CADLEdidParser::GetAdapterInfo(int iNumAdapters)
123 {
124 // validate input
125 if (iNumAdapters <= 0)
126 return NULL;
127
128 LPAdapterInfo adapterInfo = (LPAdapterInfo)malloc(sizeof(AdapterInfo) * iNumAdapters);
129 memset(adapterInfo, 0, sizeof(AdapterInfo) * iNumAdapters);
130
131 // get the info
132 ADL_Adapter_AdapterInfo_Get(adapterInfo, sizeof(AdapterInfo) * iNumAdapters);
133
134 return adapterInfo;
135 }
136
137 bool CADLEdidParser::GetAdapterEDID(int iAdapterIndex, int iDisplayIndex, ADLDisplayEDIDData *data)
138 {
139 // validate input
140 if (iAdapterIndex < 0 || iDisplayIndex < 0)
141 return false;
142
143 memset(data, 0, sizeof(ADLDisplayEDIDData));
144 data->iSize = sizeof(ADLDisplayEDIDData);
145 data->iBlockIndex = 1;
146
147 return (ADL_Display_EdidData_Get(iAdapterIndex, iDisplayIndex, data) == ADL_OK);
148 }
149
150 uint16_t CADLEdidParser::GetPhysicalAddress(void)
151 {
152 uint16_t iPA(0);
153
154 // get the number of adapters
155 int iNumAdapters = GetNumAdapters();
156 if (iNumAdapters <= 0)
157 return 0;
158
159 // get the adapter info
160 LPAdapterInfo adapterInfo = GetAdapterInfo(iNumAdapters);
161 if (!adapterInfo)
162 return 0;
163
164 // iterate over it
165 for (int iAdapterPtr = 0; iAdapterPtr < iNumAdapters; iAdapterPtr++)
166 {
167 int iNumDisplays(-1);
168 LPADLDisplayInfo displayInfo(NULL);
169 int iAdapterIndex = adapterInfo[iAdapterPtr].iAdapterIndex;
170
171 // get the display info
172 if (ADL_OK != ADL_Display_DisplayInfo_Get(iAdapterIndex, &iNumDisplays, &displayInfo, 0))
173 continue;
174
175 // iterate over it
176 for (int iDisplayPtr = 0; iDisplayPtr < iNumDisplays; iDisplayPtr++)
177 {
178 // check whether the display is connected
179 if ((displayInfo[iDisplayPtr].iDisplayInfoValue & ADL_DISPLAY_CONNECTED) != ADL_DISPLAY_CONNECTED)
180 continue;
181
182 int iDisplayIndex = displayInfo[iDisplayPtr].displayID.iDisplayLogicalIndex;
183
184 // try to get the EDID
185 ADLDisplayEDIDData edidData;
186 if (GetAdapterEDID(iAdapterIndex, iDisplayIndex, &edidData))
187 {
188 // try to get the PA from the EDID
189 iPA = CEDIDParser::GetPhysicalAddressFromEDID(edidData.cEDIDData, edidData.iEDIDSize);
190
191 // found it
192 if (iPA != 0)
193 break;
194 }
195 }
196
197 free(displayInfo);
198 }
199
200 free(adapterInfo);
201
202 return iPA;
203 }