| 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 "System/SharedLibrary.hpp" |
| 18 | |
| 19 | #define Bool int |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | template <typename FPTR> |
| 24 | void getFuncAddress(void *lib, const char *name, FPTR *out) |
| 25 | { |
| 26 | *out = reinterpret_cast<FPTR>(getProcAddress(lib, name)); |
| 27 | } |
| 28 | |
| 29 | } // anonymous namespace |
| 30 | |
| 31 | LibX11exports::LibX11exports(void *libX11, void *libXext) |
| 32 | { |
| 33 | getFuncAddress(libX11, "XOpenDisplay" , &XOpenDisplay); |
| 34 | getFuncAddress(libX11, "XGetWindowAttributes" , &XGetWindowAttributes); |
| 35 | getFuncAddress(libX11, "XDefaultScreenOfDisplay" , &XDefaultScreenOfDisplay); |
| 36 | getFuncAddress(libX11, "XWidthOfScreen" , &XWidthOfScreen); |
| 37 | getFuncAddress(libX11, "XHeightOfScreen" , &XHeightOfScreen); |
| 38 | getFuncAddress(libX11, "XPlanesOfScreen" , &XPlanesOfScreen); |
| 39 | getFuncAddress(libX11, "XDefaultGC" , &XDefaultGC); |
| 40 | getFuncAddress(libX11, "XDefaultDepth" , &XDefaultDepth); |
| 41 | getFuncAddress(libX11, "XMatchVisualInfo" , &XMatchVisualInfo); |
| 42 | getFuncAddress(libX11, "XDefaultVisual" , &XDefaultVisual); |
| 43 | getFuncAddress(libX11, "XSetErrorHandler" , &XSetErrorHandler); |
| 44 | getFuncAddress(libX11, "XSync" , &XSync); |
| 45 | getFuncAddress(libX11, "XCreateImage" , &XCreateImage); |
| 46 | getFuncAddress(libX11, "XCloseDisplay" , &XCloseDisplay); |
| 47 | getFuncAddress(libX11, "XPutImage" , &XPutImage); |
| 48 | getFuncAddress(libX11, "XDrawString" , &XDrawString); |
| 49 | |
| 50 | getFuncAddress(libXext, "XShmQueryExtension" , &XShmQueryExtension); |
| 51 | getFuncAddress(libXext, "XShmCreateImage" , &XShmCreateImage); |
| 52 | getFuncAddress(libXext, "XShmAttach" , &XShmAttach); |
| 53 | getFuncAddress(libXext, "XShmDetach" , &XShmDetach); |
| 54 | getFuncAddress(libXext, "XShmPutImage" , &XShmPutImage); |
| 55 | } |
| 56 | |
| 57 | LibX11exports *LibX11::operator->() |
| 58 | { |
| 59 | return loadExports(); |
| 60 | } |
| 61 | |
| 62 | LibX11exports *LibX11::loadExports() |
| 63 | { |
| 64 | static void *libX11 = nullptr; |
| 65 | static void *libXext = nullptr; |
| 66 | static LibX11exports *libX11exports = nullptr; |
| 67 | |
| 68 | if(!libX11) |
| 69 | { |
| 70 | if(getProcAddress(RTLD_DEFAULT, "XOpenDisplay" )) // Search the global scope for pre-loaded X11 library. |
| 71 | { |
| 72 | libX11exports = new LibX11exports(RTLD_DEFAULT, RTLD_DEFAULT); |
| 73 | libX11 = (void*)-1; // No need to load it. |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | libX11 = loadLibrary("libX11.so" ); |
| 78 | |
| 79 | if(libX11) |
| 80 | { |
| 81 | libXext = loadLibrary("libXext.so" ); |
| 82 | libX11exports = new LibX11exports(libX11, libXext); |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | libX11 = (void*)-1; // Don't attempt loading more than once. |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return libX11exports; |
| 92 | } |
| 93 | |
| 94 | LibX11 libX11; |
| 95 | |