4 * Copyright (C) 2005-2013 Team XBMC
7 * This Program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This Program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XBMC; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
27 #include "DllWaylandClient.h"
29 /* These functions are wrappers around using the wayland protocol
30 * directly. Unfortunately, since most of the protocol has autogenerated
31 * binding code, and that binding code assumes that we're directly
32 * linked to the client library (we're not), we can't use it. So
33 * we need to use wl_proxy_create, wl_proxy_marshal and wl_proxy_destory
36 * These functions effectively exist to reduce some of the duplication
37 * that surrounds using these functions directly. You should look
38 * at the autogenerated binding code to determine how wl_proxy_marshal
39 * should be used to call a particular method.
41 * Also note that there may be some cases where additional constructor
42 * or destructor functions might need to be called before or after
43 * CreateWaylandObject and DestroyWaylandObject.
45 * If you need to call a method with more than six arguments,
46 * you'll need to add a new overload here */
53 /* These functions call a method on a specifed wayland object
54 * (you should use the pointer provided by the client library
55 * and not the wrapper) with the specified opcode and
58 * THERE IS NO TYPE CHECKING so you should be especially sure that
59 * you're calling it with the right arguments. Failure to do so
60 * will result in very strange behaviour
62 template <typename Object>
63 void CallMethodOnWaylandObject(IDllWaylandClient &clientLibrary,
67 struct wl_proxy *proxy =
68 reinterpret_cast<struct wl_proxy *>(object);
69 clientLibrary.wl_proxy_marshaller()(proxy,
72 template <typename Object,
74 void CallMethodOnWaylandObject(IDllWaylandClient &clientLibrary,
79 struct wl_proxy *proxy =
80 reinterpret_cast<struct wl_proxy *>(object);
81 clientLibrary.wl_proxy_marshaller()(proxy,
86 template <typename Object,
89 void CallMethodOnWaylandObject(IDllWaylandClient &clientLibrary,
95 struct wl_proxy *proxy =
96 reinterpret_cast<struct wl_proxy *>(object);
97 clientLibrary.wl_proxy_marshaller()(proxy,
103 template <typename Object,
107 void CallMethodOnWaylandObject(IDllWaylandClient &clientLibrary,
114 struct wl_proxy *proxy =
115 reinterpret_cast<struct wl_proxy *>(object);
116 clientLibrary.wl_proxy_marshaller()(proxy,
123 template <typename Object,
128 void CallMethodOnWaylandObject(IDllWaylandClient &clientLibrary,
136 struct wl_proxy *proxy =
137 reinterpret_cast<struct wl_proxy *>(object);
138 clientLibrary.wl_proxy_marshaller()(proxy,
146 template <typename Object,
152 void CallMethodOnWaylandObject(IDllWaylandClient &clientLibrary,
161 struct wl_proxy *proxy =
162 reinterpret_cast<struct wl_proxy *>(object);
163 clientLibrary.wl_proxy_marshaller()(proxy,
172 /* This function template returns a new unmanaged object pointer
173 * as specified by Create with the RPC interface as specified
174 * as a child in the server ownership hierarchy of factory.
176 * Create must be castable to struct wl_proxy *, which
177 * means that struct wl_proxy should be its first member. Generally
178 * all wayland library proxy objects satisfy this criteria
180 template <typename Create, typename Factory>
181 Create CreateWaylandObject(IDllWaylandClient &clientLibrary,
183 struct wl_interface **interface)
185 struct wl_proxy *pfactory =
186 reinterpret_cast<struct wl_proxy *>(factory);
187 struct wl_proxy *proxy =
188 clientLibrary.wl_proxy_create(pfactory,
189 reinterpret_cast<struct wl_interface *>(interface));
193 std::stringstream ss;
194 ss << "Failed to create "
195 << typeid(Create).name()
197 << typeid(Factory).name()
200 << reinterpret_cast<void *>(pfactory)
202 throw std::runtime_error(ss.str());
205 return reinterpret_cast<Create>(proxy);
208 /* This function adds a new "listener" to the object specified.
209 * A "listener" is generally a struct of function pointers as specified
210 * by the object's RPC interface for each event it can generate. These
211 * can usually be found in the protocol header. "data" is passed
212 * to each callback to make it a full closure. */
213 template <typename Object, typename Listener>
214 int AddListenerOnWaylandObject(IDllWaylandClient &clientLibrary,
219 struct wl_proxy *proxy =
220 reinterpret_cast<struct wl_proxy *>(object);
222 /* C-style casts are bad, but there is no equavilent to
223 * std::remove_const in C++98 and we are reinterpret_cast'ing
225 IDllWaylandClient::wl_proxy_listener_func *listenerFunc =
226 (IDllWaylandClient::wl_proxy_listener_func *)((void *)listener);
227 return clientLibrary.wl_proxy_add_listener(proxy, listenerFunc, data);
230 /* This function destroys the proxy object and releases its resources
231 * on the client side. There may be an additional destroy request to
232 * release resources on the server side. That must be called
234 template <typename Object>
235 void DestroyWaylandObject(IDllWaylandClient &clientLibrary,
238 struct wl_proxy *proxy =
239 reinterpret_cast<struct wl_proxy *>(object);
240 clientLibrary.wl_proxy_destroy(proxy);