1 #include "X11Support.h"
2 
3 #include "aemu/base/SharedLibrary.h"
4 
5 #define DEFINE_DUMMY_IMPL(rettype, funcname, args) \
6     static rettype dummy_##funcname args { \
7         return (rettype)0; \
8     } \
9 
10 LIST_XLIB_FUNCTYPES(DEFINE_DUMMY_IMPL)
11 LIST_GLX_FUNCTYPES(DEFINE_DUMMY_IMPL)
12 
13 class X11FunctionGetter {
14     public:
X11FunctionGetter()15         X11FunctionGetter() :
16             mX11Lib(android::base::SharedLibrary::open("libX11")) {
17             if (!mX11Lib) {
18                 fprintf(stderr, "WARNING: could not open libX11.so, try libX11.so.6\n");
19                 mX11Lib = (android::base::SharedLibrary::open("libX11.so.6"));
20                 if (!mX11Lib) {
21                     fprintf(stderr, "ERROR: could not open libX11.so.6, give up\n");
22                     return;
23                 }
24             }
25 
26 #define X11_ASSIGN_DUMMY_IMPL(funcname) mApi.funcname = dummy_##funcname;
27 
28                 LIST_XLIB_FUNCS(X11_ASSIGN_DUMMY_IMPL)
29 
30             if (!mX11Lib) return;
31 
32 #define X11_GET_FUNC(funcname) \
33             { \
34                 auto f = mX11Lib->findSymbol(#funcname); \
35                 if (f) mApi.funcname = (funcname##_t)f; \
36             } \
37 
38                 LIST_XLIB_FUNCS(X11_GET_FUNC);
39 
40             }
41 
getApi()42         X11Api* getApi() { return &mApi; }
43     private:
44         android::base::SharedLibrary* mX11Lib;
45 
46         X11Api mApi;
47 };
48 
49 class GlxFunctionGetter {
50     public:
51         // Important: Use libGL.so.1 explicitly, because it will always link to
52         // the vendor-specific version of the library. libGL.so might in some
53         // cases, depending on bad ldconfig configurations, link to the wrapper
54         // lib that doesn't behave the same.
GlxFunctionGetter()55         GlxFunctionGetter() :
56             mGlxLib(android::base::SharedLibrary::open("libGL.so.1")) {
57 
58 #define GLX_ASSIGN_DUMMY_IMPL(funcname) mApi.funcname = dummy_##funcname;
59 
60                 LIST_GLX_FUNCS(GLX_ASSIGN_DUMMY_IMPL)
61 
62             if (!mGlxLib) return;
63 
64 #define GLX_GET_FUNC(funcname) \
65                 { \
66                 auto f = mGlxLib->findSymbol(#funcname); \
67                 if (f) mApi.funcname = (funcname##_t)f; \
68                 } \
69 
70                 LIST_GLX_FUNCS(GLX_GET_FUNC);
71 
72             }
73 
getApi()74         GlxApi* getApi() { return &mApi; }
75 
76     private:
77         android::base::SharedLibrary* mGlxLib;
78 
79         GlxApi mApi;
80 };
81 
getX11Api()82 AEMU_EXPORT struct X11Api* getX11Api() {
83     static X11FunctionGetter* g = new X11FunctionGetter;
84     return g->getApi();
85 }
86 
getGlxApi()87 AEMU_EXPORT struct GlxApi* getGlxApi() {
88     static GlxFunctionGetter* g = new GlxFunctionGetter;
89     return g->getApi();
90 }
91