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