1cb93a386Sopenharmony_ci// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2cb93a386Sopenharmony_ci//
3cb93a386Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4cb93a386Sopenharmony_ci// you may not use this file except in compliance with the License.
5cb93a386Sopenharmony_ci// You may obtain a copy of the License at
6cb93a386Sopenharmony_ci//
7cb93a386Sopenharmony_ci//    http://www.apache.org/licenses/LICENSE-2.0
8cb93a386Sopenharmony_ci//
9cb93a386Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10cb93a386Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11cb93a386Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb93a386Sopenharmony_ci// See the License for the specific language governing permissions and
13cb93a386Sopenharmony_ci// limitations under the License.
14cb93a386Sopenharmony_ci
15cb93a386Sopenharmony_ci#include "libX11.hpp"
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ci#include "Common/SharedLibrary.hpp"
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ciLibX11exports::LibX11exports(void *libX11, void *libXext)
20cb93a386Sopenharmony_ci{
21cb93a386Sopenharmony_ci	XOpenDisplay = (Display *(*)(char*))getProcAddress(libX11, "XOpenDisplay");
22cb93a386Sopenharmony_ci	XGetWindowAttributes = (Status (*)(Display*, Window, XWindowAttributes*))getProcAddress(libX11, "XGetWindowAttributes");
23cb93a386Sopenharmony_ci	XDefaultScreenOfDisplay = (Screen *(*)(Display*))getProcAddress(libX11, "XDefaultScreenOfDisplay");
24cb93a386Sopenharmony_ci	XWidthOfScreen = (int (*)(Screen*))getProcAddress(libX11, "XWidthOfScreen");
25cb93a386Sopenharmony_ci	XHeightOfScreen = (int (*)(Screen*))getProcAddress(libX11, "XHeightOfScreen");
26cb93a386Sopenharmony_ci	XPlanesOfScreen = (int (*)(Screen*))getProcAddress(libX11, "XPlanesOfScreen");
27cb93a386Sopenharmony_ci	XDefaultGC = (GC (*)(Display*, int))getProcAddress(libX11, "XDefaultGC");
28cb93a386Sopenharmony_ci	XDefaultDepth = (int (*)(Display*, int))getProcAddress(libX11, "XDefaultDepth");
29cb93a386Sopenharmony_ci	XMatchVisualInfo = (Status (*)(Display*, int, int, int, XVisualInfo*))getProcAddress(libX11, "XMatchVisualInfo");
30cb93a386Sopenharmony_ci	XDefaultVisual = (Visual *(*)(Display*, int screen_number))getProcAddress(libX11, "XDefaultVisual");
31cb93a386Sopenharmony_ci	XSetErrorHandler = (int (*(*)(int (*)(Display*, XErrorEvent*)))(Display*, XErrorEvent*))getProcAddress(libX11, "XSetErrorHandler");
32cb93a386Sopenharmony_ci	XSync = (int (*)(Display*, Bool))getProcAddress(libX11, "XSync");
33cb93a386Sopenharmony_ci	XCreateImage = (XImage *(*)(Display*, Visual*, unsigned int, int, int, char*, unsigned int, unsigned int, int, int))getProcAddress(libX11, "XCreateImage");
34cb93a386Sopenharmony_ci	XCloseDisplay = (int (*)(Display*))getProcAddress(libX11, "XCloseDisplay");
35cb93a386Sopenharmony_ci	XPutImage = (int (*)(Display*, Drawable, GC, XImage*, int, int, int, int, unsigned int, unsigned int))getProcAddress(libX11, "XPutImage");
36cb93a386Sopenharmony_ci	XDrawString = (int (*)(Display*, Drawable, GC, int, int, char*, int))getProcAddress(libX11, "XDrawString");
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ci	XShmQueryExtension = (Bool (*)(Display*))getProcAddress(libXext, "XShmQueryExtension");
39cb93a386Sopenharmony_ci	XShmCreateImage = (XImage *(*)(Display*, Visual*, unsigned int, int, char*, XShmSegmentInfo*, unsigned int, unsigned int))getProcAddress(libXext, "XShmCreateImage");
40cb93a386Sopenharmony_ci	XShmAttach = (Bool (*)(Display*, XShmSegmentInfo*))getProcAddress(libXext, "XShmAttach");
41cb93a386Sopenharmony_ci	XShmDetach = (Bool (*)(Display*, XShmSegmentInfo*))getProcAddress(libXext, "XShmDetach");
42cb93a386Sopenharmony_ci	XShmPutImage = (int (*)(Display*, Drawable, GC, XImage*, int, int, int, int, unsigned int, unsigned int, bool))getProcAddress(libXext, "XShmPutImage");
43cb93a386Sopenharmony_ci}
44cb93a386Sopenharmony_ci
45cb93a386Sopenharmony_ciLibX11exports *LibX11::operator->()
46cb93a386Sopenharmony_ci{
47cb93a386Sopenharmony_ci	return loadExports();
48cb93a386Sopenharmony_ci}
49cb93a386Sopenharmony_ci
50cb93a386Sopenharmony_ciLibX11exports *LibX11::loadExports()
51cb93a386Sopenharmony_ci{
52cb93a386Sopenharmony_ci	static void *libX11 = nullptr;
53cb93a386Sopenharmony_ci	static void *libXext = nullptr;
54cb93a386Sopenharmony_ci	static LibX11exports *libX11exports = nullptr;
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_ci	if(!libX11)
57cb93a386Sopenharmony_ci	{
58cb93a386Sopenharmony_ci		if(getProcAddress(RTLD_DEFAULT, "XOpenDisplay"))   // Search the global scope for pre-loaded X11 library.
59cb93a386Sopenharmony_ci		{
60cb93a386Sopenharmony_ci			libX11exports = new LibX11exports(RTLD_DEFAULT, RTLD_DEFAULT);
61cb93a386Sopenharmony_ci			libX11 = (void*)-1;   // No need to load it.
62cb93a386Sopenharmony_ci		}
63cb93a386Sopenharmony_ci		else
64cb93a386Sopenharmony_ci		{
65cb93a386Sopenharmony_ci			libX11 = loadLibrary("libX11.so");
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci			if(libX11)
68cb93a386Sopenharmony_ci			{
69cb93a386Sopenharmony_ci				libXext = loadLibrary("libXext.so");
70cb93a386Sopenharmony_ci				libX11exports = new LibX11exports(libX11, libXext);
71cb93a386Sopenharmony_ci			}
72cb93a386Sopenharmony_ci			else
73cb93a386Sopenharmony_ci			{
74cb93a386Sopenharmony_ci				libX11 = (void*)-1;   // Don't attempt loading more than once.
75cb93a386Sopenharmony_ci			}
76cb93a386Sopenharmony_ci		}
77cb93a386Sopenharmony_ci	}
78cb93a386Sopenharmony_ci
79cb93a386Sopenharmony_ci	return libX11exports;
80cb93a386Sopenharmony_ci}
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ciLibX11 libX11;
83