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
21LibX11exports::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 XDrawString = (int (*)(Display*, Drawable, GC, int, int, char*, int))getProcAddress(libX11, "XDrawString");
39
40 XShmQueryExtension = (Bool (*)(Display*))getProcAddress(libXext, "XShmQueryExtension");
41 XShmCreateImage = (XImage *(*)(Display*, Visual*, unsigned int, int, char*, XShmSegmentInfo*, unsigned int, unsigned int))getProcAddress(libXext, "XShmCreateImage");
42 XShmAttach = (Bool (*)(Display*, XShmSegmentInfo*))getProcAddress(libXext, "XShmAttach");
43 XShmDetach = (Bool (*)(Display*, XShmSegmentInfo*))getProcAddress(libXext, "XShmDetach");
44 XShmPutImage = (int (*)(Display*, Drawable, GC, XImage*, int, int, int, int, unsigned int, unsigned int, bool))getProcAddress(libXext, "XShmPutImage");
45}
46
47LibX11exports *LibX11::operator->()
48{
49 return loadExports();
50}
51
52LibX11exports *LibX11::loadExports()
53{
54 static void *libX11 = nullptr;
55 static void *libXext = nullptr;
56 static LibX11exports *libX11exports = nullptr;
57
58 if(!libX11)
59 {
60 if(getProcAddress(RTLD_DEFAULT, "XOpenDisplay")) // Search the global scope for pre-loaded X11 library.
61 {
62 libX11exports = new LibX11exports(RTLD_DEFAULT, RTLD_DEFAULT);
63 libX11 = (void*)-1; // No need to load it.
64 }
65 else
66 {
67 libX11 = loadLibrary("libX11.so");
68
69 if(libX11)
70 {
71 libXext = loadLibrary("libXext.so");
72 libX11exports = new LibX11exports(libX11, libXext);
73 }
74 else
75 {
76 libX11 = (void*)-1; // Don't attempt loading more than once.
77 }
78 }
79 }
80
81 return libX11exports;
82}
83
84LibX11 libX11;
85