blob: 68bd2b5fe08a2454143204fcfb07546b347c8b81 [file] [log] [blame]
Nicolas Capens68a82382018-10-02 13:16:55 -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
15#include "libX11.hpp"
16
Nicolas Capens1d8c8db2018-11-05 16:30:42 -050017#include "System/SharedLibrary.hpp"
Nicolas Capens68a82382018-10-02 13:16:55 -040018
19#define Bool int
20
Ben Claytona9af8832019-08-14 13:09:43 +010021namespace {
22
Ben Clayton45c697a2019-12-17 20:38:03 +000023template<typename FPTR>
Ben Claytona9af8832019-08-14 13:09:43 +010024void getFuncAddress(void *lib, const char *name, FPTR *out)
25{
26 *out = reinterpret_cast<FPTR>(getProcAddress(lib, name));
27}
28
Ben Clayton45c697a2019-12-17 20:38:03 +000029} // anonymous namespace
Ben Claytona9af8832019-08-14 13:09:43 +010030
Nicolas Capens68a82382018-10-02 13:16:55 -040031LibX11exports::LibX11exports(void *libX11, void *libXext)
32{
Ben Claytona9af8832019-08-14 13:09:43 +010033 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);
Nicolas Capens68a82382018-10-02 13:16:55 -040049
Ben Claytona9af8832019-08-14 13:09:43 +010050 getFuncAddress(libXext, "XShmQueryExtension", &XShmQueryExtension);
51 getFuncAddress(libXext, "XShmCreateImage", &XShmCreateImage);
52 getFuncAddress(libXext, "XShmAttach", &XShmAttach);
53 getFuncAddress(libXext, "XShmDetach", &XShmDetach);
54 getFuncAddress(libXext, "XShmPutImage", &XShmPutImage);
Nicolas Capens68a82382018-10-02 13:16:55 -040055}
56
57LibX11exports *LibX11::operator->()
58{
59 return loadExports();
60}
61
62LibX11exports *LibX11::loadExports()
63{
64 static void *libX11 = nullptr;
65 static void *libXext = nullptr;
66 static LibX11exports *libX11exports = nullptr;
67
68 if(!libX11)
69 {
Ben Clayton45c697a2019-12-17 20:38:03 +000070 if(getProcAddress(RTLD_DEFAULT, "XOpenDisplay")) // Search the global scope for pre-loaded X11 library.
Nicolas Capens68a82382018-10-02 13:16:55 -040071 {
72 libX11exports = new LibX11exports(RTLD_DEFAULT, RTLD_DEFAULT);
Ben Clayton45c697a2019-12-17 20:38:03 +000073 libX11 = (void *)-1; // No need to load it.
Nicolas Capens68a82382018-10-02 13:16:55 -040074 }
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 {
Ben Clayton45c697a2019-12-17 20:38:03 +000086 libX11 = (void *)-1; // Don't attempt loading more than once.
Nicolas Capens68a82382018-10-02 13:16:55 -040087 }
88 }
89 }
90
91 return libX11exports;
92}
93
94LibX11 libX11;