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
LibX11exports(void * libX11,void * libXext)19 LibX11exports::LibX11exports(void *libX11, void *libXext)
20 {
21 XOpenDisplay = (Display *(*)(char*))getProcAddress(libX11, "XOpenDisplay");
22 XGetWindowAttributes = (Status (*)(Display*, Window, XWindowAttributes*))getProcAddress(libX11, "XGetWindowAttributes");
23 XDefaultScreenOfDisplay = (Screen *(*)(Display*))getProcAddress(libX11, "XDefaultScreenOfDisplay");
24 XWidthOfScreen = (int (*)(Screen*))getProcAddress(libX11, "XWidthOfScreen");
25 XHeightOfScreen = (int (*)(Screen*))getProcAddress(libX11, "XHeightOfScreen");
26 XPlanesOfScreen = (int (*)(Screen*))getProcAddress(libX11, "XPlanesOfScreen");
27 XDefaultGC = (GC (*)(Display*, int))getProcAddress(libX11, "XDefaultGC");
28 XDefaultDepth = (int (*)(Display*, int))getProcAddress(libX11, "XDefaultDepth");
29 XMatchVisualInfo = (Status (*)(Display*, int, int, int, XVisualInfo*))getProcAddress(libX11, "XMatchVisualInfo");
30 XDefaultVisual = (Visual *(*)(Display*, int screen_number))getProcAddress(libX11, "XDefaultVisual");
31 XSetErrorHandler = (int (*(*)(int (*)(Display*, XErrorEvent*)))(Display*, XErrorEvent*))getProcAddress(libX11, "XSetErrorHandler");
32 XSync = (int (*)(Display*, Bool))getProcAddress(libX11, "XSync");
33 XCreateImage = (XImage *(*)(Display*, Visual*, unsigned int, int, int, char*, unsigned int, unsigned int, int, int))getProcAddress(libX11, "XCreateImage");
34 XCloseDisplay = (int (*)(Display*))getProcAddress(libX11, "XCloseDisplay");
35 XPutImage = (int (*)(Display*, Drawable, GC, XImage*, int, int, int, int, unsigned int, unsigned int))getProcAddress(libX11, "XPutImage");
36 XDrawString = (int (*)(Display*, Drawable, GC, int, int, char*, int))getProcAddress(libX11, "XDrawString");
37
38 XShmQueryExtension = (Bool (*)(Display*))getProcAddress(libXext, "XShmQueryExtension");
39 XShmCreateImage = (XImage *(*)(Display*, Visual*, unsigned int, int, char*, XShmSegmentInfo*, unsigned int, unsigned int))getProcAddress(libXext, "XShmCreateImage");
40 XShmAttach = (Bool (*)(Display*, XShmSegmentInfo*))getProcAddress(libXext, "XShmAttach");
41 XShmDetach = (Bool (*)(Display*, XShmSegmentInfo*))getProcAddress(libXext, "XShmDetach");
42 XShmPutImage = (int (*)(Display*, Drawable, GC, XImage*, int, int, int, int, unsigned int, unsigned int, bool))getProcAddress(libXext, "XShmPutImage");
43 }
44
operator ->()45 LibX11exports *LibX11::operator->()
46 {
47 return loadExports();
48 }
49
loadExports()50 LibX11exports *LibX11::loadExports()
51 {
52 static void *libX11 = nullptr;
53 static void *libXext = nullptr;
54 static LibX11exports *libX11exports = nullptr;
55
56 if(!libX11)
57 {
58 if(getProcAddress(RTLD_DEFAULT, "XOpenDisplay")) // Search the global scope for pre-loaded X11 library.
59 {
60 libX11exports = new LibX11exports(RTLD_DEFAULT, RTLD_DEFAULT);
61 libX11 = (void*)-1; // No need to load it.
62 }
63 else
64 {
65 libX11 = loadLibrary("libX11.so");
66
67 if(libX11)
68 {
69 libXext = loadLibrary("libXext.so");
70 libX11exports = new LibX11exports(libX11, libXext);
71 }
72 else
73 {
74 libX11 = (void*)-1; // Don't attempt loading more than once.
75 }
76 }
77 }
78
79 return libX11exports;
80 }
81
82 LibX11 libX11;
83