blob: 7711d12301f59e9a60070aa0920de3e7264beb90 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// 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 Capensa36c9902015-04-13 03:51:45 -040015#include "libX11.hpp"
16
17#include "Common/SharedLibrary.hpp"
18
Nicolas Capensa36c9902015-04-13 03:51:45 -040019LibX11exports::LibX11exports(void *libX11, void *libXext)
20{
Nicolas Capens0bac2852016-05-07 06:09:58 -040021 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 Capensbba05f32017-09-15 14:59:38 -040036 XDrawString = (int (*)(Display*, Drawable, GC, int, int, char*, int))getProcAddress(libX11, "XDrawString");
Nicolas Capensa36c9902015-04-13 03:51:45 -040037
Nicolas Capens0bac2852016-05-07 06:09:58 -040038 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 Capensa36c9902015-04-13 03:51:45 -040043}
44
45LibX11exports *LibX11::operator->()
46{
Nicolas Capens82196a22015-04-20 13:50:37 -040047 return loadExports();
48}
49
50LibX11exports *LibX11::loadExports()
51{
Nicolas Capens0bac2852016-05-07 06:09:58 -040052 static void *libX11 = nullptr;
53 static void *libXext = nullptr;
54 static LibX11exports *libX11exports = nullptr;
Nicolas Capensa36c9902015-04-13 03:51:45 -040055
Nicolas Capens0bac2852016-05-07 06:09:58 -040056 if(!libX11)
57 {
Nicolas Capensac213e32016-12-21 22:50:26 -050058 if(getProcAddress(RTLD_DEFAULT, "XOpenDisplay")) // Search the global scope for pre-loaded X11 library.
Nicolas Capens0bac2852016-05-07 06:09:58 -040059 {
Nicolas Capensac213e32016-12-21 22:50:26 -050060 libX11exports = new LibX11exports(RTLD_DEFAULT, RTLD_DEFAULT);
61 libX11 = (void*)-1; // No need to load it.
Nicolas Capens0bac2852016-05-07 06:09:58 -040062 }
Nicolas Capensac213e32016-12-21 22:50:26 -050063 else
Nicolas Capens0bac2852016-05-07 06:09:58 -040064 {
Nicolas Capensac213e32016-12-21 22:50:26 -050065 libX11 = loadLibrary("libX11.so");
Nicolas Capense7dee542015-05-06 19:41:43 -040066
Nicolas Capensac213e32016-12-21 22:50:26 -050067 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 Capens0bac2852016-05-07 06:09:58 -040076 }
77 }
Nicolas Capensa36c9902015-04-13 03:51:45 -040078
Nicolas Capens0bac2852016-05-07 06:09:58 -040079 return libX11exports;
Nicolas Capensa36c9902015-04-13 03:51:45 -040080}
81
82LibX11 libX11;