7a7dce6cf583d70c340e7f1fc9f5d0bcd5c9a595
[deb_libcec.git] / include / cecloader.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
32 */
33
34 #ifndef CECLOADER_H_
35 #define CECLOADER_H_
36
37 #if defined(_WIN32) || defined(_WIN64)
38 #include <windows.h>
39 #include <conio.h>
40
41 HINSTANCE g_libCEC = NULL;
42
43 /*!
44 * @brief Create a new libCEC instance.
45 * @param strDeviceName The name of the primary device to pass to other CEC devices.
46 * @param types The list of device types to register on the bus.
47 * @param strLib The name of and/or path to libCEC
48 * @return An instance of libCEC or NULL when it failed to load.
49 */
50 CEC::ICECAdapter *LibCecInit(const char *strDeviceName, CEC::cec_device_type_list types, const char *strLib = NULL)
51 {
52 if (!g_libCEC)
53 #if defined(_WIN64)
54 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.x64.dll");
55 #else
56 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.dll");
57 #endif
58 if (!g_libCEC)
59 return NULL;
60
61 typedef void* (__cdecl*_LibCecInit)(const char *, CEC::cec_device_type_list);
62 _LibCecInit LibCecInit;
63 LibCecInit = (_LibCecInit) (GetProcAddress(g_libCEC, "CECInit"));
64 if (!LibCecInit)
65 return NULL;
66 return static_cast< CEC::ICECAdapter* > (LibCecInit(strDeviceName, types));
67 }
68
69 /*!
70 * @brief Create a new libCEC instance.
71 * @param configuration The configuration to pass to libCEC
72 * @param strLib The name of and/or path to libCEC
73 * @return An instance of ICECAdapter or NULL on error.
74 */
75 CEC::ICECAdapter *LibCecInitialise(CEC::libcec_configuration *configuration, const char *strLib = NULL)
76 {
77 if (!g_libCEC)
78 #if defined(_WIN64)
79 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.x64.dll");
80 #else
81 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.dll");
82 #endif
83 if (!g_libCEC)
84 return NULL;
85
86 typedef void* (__cdecl*_LibCecInitialise)(CEC::libcec_configuration *);
87 _LibCecInitialise LibCecInitialise;
88 LibCecInitialise = (_LibCecInitialise) (GetProcAddress(g_libCEC, "CECInitialise"));
89 if (!LibCecInitialise)
90 {
91 cout << "cannot find CECInitialise" << endl;
92 return NULL;
93 }
94
95 return static_cast< CEC::ICECAdapter* > (LibCecInitialise(configuration));
96 }
97
98 /*!
99 * @brief Destroy an instance of libCEC.
100 * @param device The instance to destroy.
101 */
102 void UnloadLibCec(CEC::ICECAdapter *device)
103 {
104 typedef void (__cdecl*_DestroyLibCec)(void * device);
105 _DestroyLibCec DestroyLibCec;
106 DestroyLibCec = (_DestroyLibCec) (GetProcAddress(g_libCEC, "CECDestroy"));
107 if (DestroyLibCec)
108 DestroyLibCec(device);
109
110 FreeLibrary(g_libCEC);
111 g_libCEC = NULL;
112 }
113
114 /*!
115 * @brief Start the bootloader on the first device that was detected.
116 * @param strLib The name of and/or path to libCEC
117 * @return True when the command was sent, false otherwise.
118 */
119 bool LibCecBootloader(const char *strLib = NULL)
120 {
121 if (!g_libCEC)
122 #if defined(_WIN64)
123 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.x64.dll");
124 #else
125 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.dll");
126 #endif
127 if (!g_libCEC)
128 return NULL;
129
130 typedef bool (__cdecl*_LibCecBootloader)(void);
131 _LibCecBootloader LibCecBootloader;
132 LibCecBootloader = (_LibCecBootloader) (GetProcAddress(g_libCEC, "CECStartBootloader"));
133 if (!LibCecBootloader)
134 return false;
135
136 bool bReturn = LibCecBootloader();
137 FreeLibrary(g_libCEC);
138 g_libCEC = NULL;
139 return bReturn;
140 }
141
142 #else
143
144 #include <dlfcn.h>
145
146 void *g_libCEC = NULL;
147
148 /*!
149 * @brief Create a new libCEC instance.
150 * @param strDeviceName The name of the primary device to pass to other CEC devices.
151 * @param types The list of device types to register on the bus.
152 * @param strLib The name of and/or path to libCEC
153 * @return An instance of libCEC or NULL when it failed to load.
154 */
155 CEC::ICECAdapter *LibCecInit(const char *strDeviceName, CEC::cec_device_type_list types, const char *strLib = NULL)
156 {
157 if (!g_libCEC)
158 {
159 #if defined(__APPLE__)
160 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
161 #else
162 g_libCEC = dlopen(strLib ? strLib : "libcec.so." CEC_LIB_VERSION_MAJOR_STR, RTLD_LAZY);
163 #endif
164 if (!g_libCEC)
165 {
166 cout << dlerror() << endl;
167 return NULL;
168 }
169 }
170
171 typedef void* _LibCecInit(const char *, CEC::cec_device_type_list);
172 _LibCecInit* LibCecInit = (_LibCecInit*) dlsym(g_libCEC, "CECInit");
173 if (!LibCecInit)
174 {
175 cout << "cannot find CECInit" << endl;
176 return NULL;
177 }
178
179 return (CEC::ICECAdapter*) LibCecInit(strDeviceName, types);
180 }
181
182 /*!
183 * @brief Create a new libCEC instance.
184 * @param configuration The configuration to pass to libCEC
185 * @param strLib The name of and/or path to libCEC
186 * @return An instance of ICECAdapter or NULL on error.
187 */
188 CEC::ICECAdapter *LibCecInitialise(CEC::libcec_configuration *configuration, const char *strLib = NULL)
189 {
190 if (!g_libCEC)
191 {
192 #if defined(__APPLE__)
193 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
194 #else
195 g_libCEC = dlopen(strLib ? strLib : "libcec.so." CEC_LIB_VERSION_MAJOR_STR, RTLD_LAZY);
196 #endif
197 if (!g_libCEC)
198 {
199 cout << dlerror() << endl;
200 return NULL;
201 }
202 }
203
204 typedef void* _LibCecInitialise(CEC::libcec_configuration *);
205 _LibCecInitialise* LibCecInitialise = (_LibCecInitialise*) dlsym(g_libCEC, "CECInitialise");
206 if (!LibCecInitialise)
207 {
208 cout << "cannot find CECInitialise" << endl;
209 return NULL;
210 }
211
212 return (CEC::ICECAdapter*) LibCecInitialise(configuration);
213 }
214
215 /*!
216 * @brief Destroy an instance of libCEC.
217 * @param device The instance to destroy.
218 */
219 void UnloadLibCec(CEC::ICECAdapter *device)
220 {
221 typedef void* _DestroyLibCec(CEC::ICECAdapter *);
222 _DestroyLibCec *DestroyLibCec = (_DestroyLibCec*) dlsym(g_libCEC, "CECDestroy");
223 if (DestroyLibCec)
224 DestroyLibCec(device);
225
226 dlclose(g_libCEC);
227 }
228
229 /*!
230 * @brief Start the bootloader on the first device that was detected.
231 * @param strLib The name of and/or path to libCEC
232 * @return True when the command was sent, false otherwise.
233 */
234 bool LibCecBootloader(const char *strLib = NULL)
235 {
236 if (!g_libCEC)
237 {
238 #if defined(__APPLE__)
239 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
240 #else
241 g_libCEC = dlopen(strLib ? strLib : "libcec.so." CEC_LIB_VERSION_MAJOR_STR, RTLD_LAZY);
242 #endif
243 if (!g_libCEC)
244 {
245 cout << dlerror() << endl;
246 return NULL;
247 }
248 }
249
250 typedef bool _LibCecBootloader(void);
251 _LibCecBootloader* LibCecBootloader = (_LibCecBootloader*) dlsym(g_libCEC, "CECStartBootloader");
252 if (!LibCecBootloader)
253 {
254 cout << "cannot find CECStartBootloader" << endl;
255 return NULL;
256 }
257
258 bool bReturn = LibCecBootloader();
259 dlclose(g_libCEC);
260 return bReturn;
261 }
262
263 #endif
264
265 #endif /* CECLOADER_H_ */