1*03ce13f7SAndroid Build Coastguard Worker // Copyright 2021 The SwiftShader Authors. All Rights Reserved.
2*03ce13f7SAndroid Build Coastguard Worker //
3*03ce13f7SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*03ce13f7SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*03ce13f7SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*03ce13f7SAndroid Build Coastguard Worker //
7*03ce13f7SAndroid Build Coastguard Worker // http://www.apache.org/licenses/LICENSE-2.0
8*03ce13f7SAndroid Build Coastguard Worker //
9*03ce13f7SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*03ce13f7SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*03ce13f7SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*03ce13f7SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*03ce13f7SAndroid Build Coastguard Worker // limitations under the License.
14*03ce13f7SAndroid Build Coastguard Worker
15*03ce13f7SAndroid Build Coastguard Worker #include "libWaylandClient.hpp"
16*03ce13f7SAndroid Build Coastguard Worker
17*03ce13f7SAndroid Build Coastguard Worker #include "System/SharedLibrary.hpp"
18*03ce13f7SAndroid Build Coastguard Worker
19*03ce13f7SAndroid Build Coastguard Worker #include <memory>
20*03ce13f7SAndroid Build Coastguard Worker
LibWaylandClientExports(void * libwl)21*03ce13f7SAndroid Build Coastguard Worker LibWaylandClientExports::LibWaylandClientExports(void *libwl)
22*03ce13f7SAndroid Build Coastguard Worker {
23*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_display_dispatch", &wl_display_dispatch);
24*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_display_get_registry", &wl_display_get_registry);
25*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_display_roundtrip", &wl_display_roundtrip);
26*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_display_sync", &wl_display_sync);
27*03ce13f7SAndroid Build Coastguard Worker
28*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_registry_add_listener", &wl_registry_add_listener);
29*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_registry_bind", &wl_registry_bind);
30*03ce13f7SAndroid Build Coastguard Worker
31*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_buffer_destroy", &wl_buffer_destroy);
32*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_shm_create_pool", &wl_shm_create_pool);
33*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_shm_pool_create_buffer", &wl_shm_pool_create_buffer);
34*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_shm_pool_destroy", &wl_shm_pool_destroy);
35*03ce13f7SAndroid Build Coastguard Worker
36*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_surface_attach", &wl_surface_attach);
37*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_surface_damage", &wl_surface_damage);
38*03ce13f7SAndroid Build Coastguard Worker getFuncAddress(libwl, "wl_surface_commit", &wl_surface_commit);
39*03ce13f7SAndroid Build Coastguard Worker
40*03ce13f7SAndroid Build Coastguard Worker wl_shm_interface = reinterpret_cast<const wl_interface *>(getProcAddress(libwl, "wl_shm_interface"));
41*03ce13f7SAndroid Build Coastguard Worker }
42*03ce13f7SAndroid Build Coastguard Worker
operator ->()43*03ce13f7SAndroid Build Coastguard Worker LibWaylandClientExports *LibWaylandClient::operator->()
44*03ce13f7SAndroid Build Coastguard Worker {
45*03ce13f7SAndroid Build Coastguard Worker return loadExports();
46*03ce13f7SAndroid Build Coastguard Worker }
47*03ce13f7SAndroid Build Coastguard Worker
loadExports()48*03ce13f7SAndroid Build Coastguard Worker LibWaylandClientExports *LibWaylandClient::loadExports()
49*03ce13f7SAndroid Build Coastguard Worker {
50*03ce13f7SAndroid Build Coastguard Worker static LibWaylandClientExports exports = [] {
51*03ce13f7SAndroid Build Coastguard Worker void *libwl = nullptr;
52*03ce13f7SAndroid Build Coastguard Worker
53*03ce13f7SAndroid Build Coastguard Worker if(getProcAddress(RTLD_DEFAULT, "wl_display_dispatch")) // Search the global scope for pre-loaded Wayland client library.
54*03ce13f7SAndroid Build Coastguard Worker {
55*03ce13f7SAndroid Build Coastguard Worker libwl = RTLD_DEFAULT;
56*03ce13f7SAndroid Build Coastguard Worker }
57*03ce13f7SAndroid Build Coastguard Worker else
58*03ce13f7SAndroid Build Coastguard Worker {
59*03ce13f7SAndroid Build Coastguard Worker libwl = loadLibrary("libwayland-client.so.0");
60*03ce13f7SAndroid Build Coastguard Worker }
61*03ce13f7SAndroid Build Coastguard Worker
62*03ce13f7SAndroid Build Coastguard Worker return LibWaylandClientExports(libwl);
63*03ce13f7SAndroid Build Coastguard Worker }();
64*03ce13f7SAndroid Build Coastguard Worker
65*03ce13f7SAndroid Build Coastguard Worker return exports.wl_display_dispatch ? &exports : nullptr;
66*03ce13f7SAndroid Build Coastguard Worker }
67*03ce13f7SAndroid Build Coastguard Worker
68*03ce13f7SAndroid Build Coastguard Worker LibWaylandClient libWaylandClient;
69