summaryrefslogtreecommitdiff
path: root/va/x11/va_fglrx.c
blob: 5be02569eb174707da051c44987e99d48de122a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (C) 2010 Splitted-Desktop Systems. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <X11/Xlib.h>

#define ADL_OK 0
#define ADL_MAX_PATH 256

/*
 * Based on public AMD Display Library (ADL) SDK:
 * <http://developer.amd.com/gpu/adlsdk/Pages/default.aspx>
 */
typedef struct AdapterInfo {
    int iSize;
    int iAdapterIndex;
    char strUDID[ADL_MAX_PATH]; 
    int iBusNumber;
    int iDeviceNumber;
    int iFunctionNumber;
    int iVendorID;
    char strAdapterName[ADL_MAX_PATH];
    char strDisplayName[ADL_MAX_PATH];
    int iPresent;
    int iXScreenNum;
    int iDrvIndex;
    char strXScreenConfigName[ADL_MAX_PATH];
} AdapterInfo, *LPAdapterInfo;

typedef struct XScreenInfo {
    int iXScreenNum;
    char strXScreenConfigName[ADL_MAX_PATH];
} XScreenInfo, *LPXScreenInfo;

typedef void *(*ADL_MAIN_MALLOC_CALLBACK)(int);
typedef int (*ADL_MAIN_CONTROL_CREATE)(ADL_MAIN_MALLOC_CALLBACK, int);
typedef int (*ADL_MAIN_CONTROL_DESTROY)(void);
typedef int (*ADL_ADAPTER_NUMBEROFADAPTERS_GET)(int *);
typedef int (*ADL_ADAPTER_ADAPTERINFO_GET)(LPAdapterInfo, int);
typedef int (*ADL_ADAPTER_XSCREENINFO_GET)(LPXScreenInfo, int);

static void *ADL_Main_Memory_Alloc(int iSize)
{
    return malloc(iSize);
}

static void ADL_Main_Memory_Free(void *arg)
{
    void ** const lpBuffer = arg;

    if (lpBuffer && *lpBuffer) {
        free(*lpBuffer);
        *lpBuffer = NULL;
    }
}

static int match_display(Display *x11_dpy, const char *display_name)
{
    Display *test_dpy;
    char *test_dpy_name, *x11_dpy_name;
    int m;

    test_dpy = XOpenDisplay(display_name);
    if (!test_dpy)
        return 0;

    test_dpy_name = XDisplayString(test_dpy);
    x11_dpy_name  = XDisplayString(x11_dpy);

    if (x11_dpy_name && test_dpy_name)
        m = strcmp(x11_dpy_name, test_dpy_name) == 0;
    else
        m = !x11_dpy_name && !test_dpy_name;

    XCloseDisplay(test_dpy);
    return m;
}

Bool VA_FGLRXGetClientDriverName( Display *dpy, int screen,
    int *ddxDriverMajorVersion, int *ddxDriverMinorVersion,
    int *ddxDriverPatchVersion, char **clientDriverName )
{
    ADL_MAIN_CONTROL_CREATE          ADL_Main_Control_Create;
    ADL_MAIN_CONTROL_DESTROY         ADL_Main_Control_Destroy;
    ADL_ADAPTER_NUMBEROFADAPTERS_GET ADL_Adapter_NumberOfAdapters_Get;
    ADL_ADAPTER_ADAPTERINFO_GET      ADL_Adapter_AdapterInfo_Get;
    ADL_ADAPTER_XSCREENINFO_GET      ADL_Adapter_XScreenInfo_Get;

    LPAdapterInfo lpAdapterInfo = NULL;
    LPXScreenInfo lpXScreenInfo = NULL;
    void *libadl_handle = NULL;
    Bool success = False;
    int is_adl_initialized = 0;
    int i, num_adapters, lpAdapterInfo_size, lpXScreenInfo_size;

    if (ddxDriverMajorVersion)
        *ddxDriverMajorVersion = 0;
    if (ddxDriverMinorVersion)
        *ddxDriverMinorVersion = 0;
    if (ddxDriverPatchVersion)
        *ddxDriverPatchVersion = 0;
    if (clientDriverName)
        *clientDriverName = NULL;

    libadl_handle = dlopen("libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
    if (!libadl_handle)
        goto end;

    dlerror();
    ADL_Main_Control_Create = (ADL_MAIN_CONTROL_CREATE)
        dlsym(libadl_handle,"ADL_Main_Control_Create");
    if (dlerror())
        goto end;

    ADL_Main_Control_Destroy = (ADL_MAIN_CONTROL_DESTROY)
        dlsym(libadl_handle,"ADL_Main_Control_Destroy");
    if (dlerror())
        goto end;

    ADL_Adapter_NumberOfAdapters_Get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET)
        dlsym(libadl_handle,"ADL_Adapter_NumberOfAdapters_Get");
    if (dlerror())
        goto end;

    ADL_Adapter_AdapterInfo_Get = (ADL_ADAPTER_ADAPTERINFO_GET)
        dlsym(libadl_handle,"ADL_Adapter_AdapterInfo_Get");
    if (dlerror())
        goto end;

    ADL_Adapter_XScreenInfo_Get = (ADL_ADAPTER_XSCREENINFO_GET)
        dlsym(libadl_handle,"ADL_Adapter_XScreenInfo_Get");
    if (dlerror())
        goto end;

    if (ADL_Main_Control_Create(ADL_Main_Memory_Alloc, 1) != ADL_OK)
        goto end;
    is_adl_initialized = 1;

    if (ADL_Adapter_NumberOfAdapters_Get(&num_adapters) != ADL_OK)
        goto end;
    if (num_adapters <= 0)
        goto end;

    lpAdapterInfo_size = num_adapters * sizeof(*lpAdapterInfo);
    lpAdapterInfo = ADL_Main_Memory_Alloc(lpAdapterInfo_size);
    if (!lpAdapterInfo)
        goto end;
    memset(lpAdapterInfo, 0, lpAdapterInfo_size);

    for (i = 0; i < num_adapters; i++)
        lpAdapterInfo[i].iSize = sizeof(lpAdapterInfo[i]);

    lpXScreenInfo_size = num_adapters * sizeof(*lpXScreenInfo);
    lpXScreenInfo = ADL_Main_Memory_Alloc(lpXScreenInfo_size);
    if (!lpXScreenInfo)
        goto end;
    memset(lpXScreenInfo, 0, lpXScreenInfo_size);

    if (ADL_Adapter_AdapterInfo_Get(lpAdapterInfo, lpAdapterInfo_size) != ADL_OK)
        goto end;

    if (ADL_Adapter_XScreenInfo_Get(lpXScreenInfo, lpXScreenInfo_size) != ADL_OK)
        goto end;

    for (i = 0; i < num_adapters; i++) {
        LPXScreenInfo const lpCurrXScreenInfo = &lpXScreenInfo[i];
        LPAdapterInfo const lpCurrAdapterInfo = &lpAdapterInfo[i];
        if (!lpCurrAdapterInfo->iPresent)
            continue;
#if 0
        printf("Adapter %d:\n", i);
        printf("  iAdapterIndex: %d\n",    lpCurrAdapterInfo->iAdapterIndex);
        printf("  strUDID: '%s'\n",        lpCurrAdapterInfo->strUDID);
        printf("  iBusNumber: %d\n",       lpCurrAdapterInfo->iBusNumber);
        printf("  iDeviceNumber: %d\n",    lpCurrAdapterInfo->iDeviceNumber);
        printf("  iFunctionNumber: %d\n",  lpCurrAdapterInfo->iFunctionNumber);
        printf("  iVendorID: 0x%04x\n",    lpCurrAdapterInfo->iVendorID);
        printf("  strAdapterName: '%s'\n", lpCurrAdapterInfo->strAdapterName);
        printf("  strDisplayName: '%s'\n", lpCurrAdapterInfo->strDisplayName);
        printf("  iPresent: %d\n",         lpCurrAdapterInfo->iPresent);
        printf("  iXScreenNum: %d\n",      lpCurrXScreenInfo->iXScreenNum);
#endif
        if (match_display(dpy, lpCurrAdapterInfo->strDisplayName) &&
            screen == lpCurrXScreenInfo->iXScreenNum) {
            *clientDriverName = strdup("fglrx");
            break;
        }
    }

    success = True;
end:
    if (lpXScreenInfo)
        ADL_Main_Memory_Free(&lpXScreenInfo);
    if (lpAdapterInfo)
        ADL_Main_Memory_Free(&lpAdapterInfo);
    if (is_adl_initialized)
        ADL_Main_Control_Destroy();
    if (libadl_handle)
        dlclose(libadl_handle);
    return success;
}