Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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 | |
Nicolas Capens | a36c990 | 2015-04-13 03:51:45 -0400 | [diff] [blame] | 15 | #include "libX11.hpp" |
| 16 | |
| 17 | #include "Common/SharedLibrary.hpp" |
| 18 | |
Nicolas Capens | a36c990 | 2015-04-13 03:51:45 -0400 | [diff] [blame] | 19 | LibX11exports::LibX11exports(void *libX11, void *libXext) |
| 20 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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"); |
Nicolas Capens | bba05f3 | 2017-09-15 14:59:38 -0400 | [diff] [blame] | 36 | XDrawString = (int (*)(Display*, Drawable, GC, int, int, char*, int))getProcAddress(libX11, "XDrawString"); |
Nicolas Capens | a36c990 | 2015-04-13 03:51:45 -0400 | [diff] [blame] | 37 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 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"); |
Nicolas Capens | a36c990 | 2015-04-13 03:51:45 -0400 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | LibX11exports *LibX11::operator->() |
| 46 | { |
Nicolas Capens | 82196a2 | 2015-04-20 13:50:37 -0400 | [diff] [blame] | 47 | return loadExports(); |
| 48 | } |
| 49 | |
| 50 | LibX11exports *LibX11::loadExports() |
| 51 | { |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 52 | static void *libX11 = nullptr; |
| 53 | static void *libXext = nullptr; |
| 54 | static LibX11exports *libX11exports = nullptr; |
Nicolas Capens | a36c990 | 2015-04-13 03:51:45 -0400 | [diff] [blame] | 55 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 56 | if(!libX11) |
| 57 | { |
Nicolas Capens | ac213e3 | 2016-12-21 22:50:26 -0500 | [diff] [blame] | 58 | if(getProcAddress(RTLD_DEFAULT, "XOpenDisplay")) // Search the global scope for pre-loaded X11 library. |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 59 | { |
Nicolas Capens | ac213e3 | 2016-12-21 22:50:26 -0500 | [diff] [blame] | 60 | libX11exports = new LibX11exports(RTLD_DEFAULT, RTLD_DEFAULT); |
| 61 | libX11 = (void*)-1; // No need to load it. |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 62 | } |
Nicolas Capens | ac213e3 | 2016-12-21 22:50:26 -0500 | [diff] [blame] | 63 | else |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 64 | { |
Nicolas Capens | ac213e3 | 2016-12-21 22:50:26 -0500 | [diff] [blame] | 65 | libX11 = loadLibrary("libX11.so"); |
Nicolas Capens | e7dee54 | 2015-05-06 19:41:43 -0400 | [diff] [blame] | 66 | |
Nicolas Capens | ac213e3 | 2016-12-21 22:50:26 -0500 | [diff] [blame] | 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 | } |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 76 | } |
| 77 | } |
Nicolas Capens | a36c990 | 2015-04-13 03:51:45 -0400 | [diff] [blame] | 78 | |
Nicolas Capens | 0bac285 | 2016-05-07 06:09:58 -0400 | [diff] [blame] | 79 | return libX11exports; |
Nicolas Capens | a36c990 | 2015-04-13 03:51:45 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | LibX11 libX11; |