cec: added a command to the interface to start the bootloader directly, without going...
[deb_libcec.git] / include / cecloader.h
CommitLineData
f00ff009 1#pragma once
acec5f48
LOK
2/*
3 * This file is part of the libCEC(R) library.
4 *
b492c10e 5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
acec5f48
LOK
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
41HINSTANCE g_libCEC = NULL;
42
53024cc1
LOK
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 */
d2eadc31
LOK
50CEC::ICECAdapter *LibCecInit(const char *strDeviceName, CEC::cec_device_type_list types, const char *strLib = NULL)
51{
52 if (!g_libCEC)
f00ff009
LOK
53#if defined(_WIN64)
54 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.x64.dll");
55#else
d2eadc31 56 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.dll");
f00ff009 57#endif
d2eadc31
LOK
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
caca2d81
LOK
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 */
3efda01a 75CEC::ICECAdapter *LibCecInitialise(CEC::libcec_configuration *configuration, const char *strLib = NULL)
caca2d81
LOK
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
3efda01a 86 typedef void* (__cdecl*_LibCecInitialise)(CEC::libcec_configuration *);
caca2d81
LOK
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
53024cc1
LOK
98/*!
99 * @brief Destroy an instance of libCEC.
100 * @param device The instance to destroy.
101 */
acec5f48
LOK
102void 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
a2198e5e
LOK
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 */
119bool 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 UnloadLibCec(static_cast< CEC::ICECAdapter* > g_libCEC);
138 return bReturn;
139}
140
acec5f48
LOK
141#else
142
143#include <dlfcn.h>
144
145void *g_libCEC = NULL;
146
53024cc1 147/*!
caca2d81
LOK
148 * @brief Create a new libCEC instance.
149 * @param strDeviceName The name of the primary device to pass to other CEC devices.
150 * @param types The list of device types to register on the bus.
151 * @param strLib The name of and/or path to libCEC
152 * @return An instance of libCEC or NULL when it failed to load.
53024cc1 153 */
caca2d81 154CEC::ICECAdapter *LibCecInit(const char *strDeviceName, CEC::cec_device_type_list types, const char *strLib = NULL)
acec5f48 155{
acec5f48
LOK
156 if (!g_libCEC)
157 {
30a57ee8 158#if defined(__APPLE__)
dad7a92e 159 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
30a57ee8 160#else
dad7a92e 161 g_libCEC = dlopen(strLib ? strLib : "libcec.so", RTLD_LAZY);
30a57ee8 162#endif
acec5f48
LOK
163 if (!g_libCEC)
164 {
30a57ee8 165#if defined(__APPLE__)
e9de9629 166 cout << "cannot find " << (strLib ? strLib : "libcec.dylib") << dlerror() << endl;
30a57ee8 167#else
e9de9629 168 cout << "cannot find " << (strLib ? strLib : "libcec.so") << dlerror() << endl;
30a57ee8 169#endif
acec5f48
LOK
170 return NULL;
171 }
172 }
173
caca2d81
LOK
174 typedef void* _LibCecInit(const char *, CEC::cec_device_type_list);
175 _LibCecInit* LibCecInit = (_LibCecInit*) dlsym(g_libCEC, "CECInit");
176 if (!LibCecInit)
acec5f48 177 {
caca2d81 178 cout << "cannot find CECInit" << endl;
acec5f48
LOK
179 return NULL;
180 }
181
caca2d81 182 return (CEC::ICECAdapter*) LibCecInit(strDeviceName, types);
acec5f48
LOK
183}
184
53024cc1
LOK
185/*!
186 * @brief Create a new libCEC instance.
caca2d81 187 * @param configuration The configuration to pass to libCEC
39b1216c 188 * @param strLib The name of and/or path to libCEC
caca2d81 189 * @return An instance of ICECAdapter or NULL on error.
53024cc1 190 */
3efda01a 191CEC::ICECAdapter *LibCecInitialise(CEC::libcec_configuration *configuration, const char *strLib = NULL)
f8513317
LOK
192{
193 if (!g_libCEC)
194 {
195#if defined(__APPLE__)
196 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
197#else
198 g_libCEC = dlopen(strLib ? strLib : "libcec.so", RTLD_LAZY);
199#endif
200 if (!g_libCEC)
201 {
202#if defined(__APPLE__)
203 cout << "cannot find " << (strLib ? strLib : "libcec.dylib") << dlerror() << endl;
204#else
205 cout << "cannot find " << (strLib ? strLib : "libcec.so") << dlerror() << endl;
206#endif
207 return NULL;
208 }
209 }
210
3efda01a 211 typedef void* _LibCecInitialise(CEC::libcec_configuration *);
caca2d81
LOK
212 _LibCecInitialise* LibCecInitialise = (_LibCecInitialise*) dlsym(g_libCEC, "CECInitialise");
213 if (!LibCecInitialise)
f8513317 214 {
caca2d81 215 cout << "cannot find CECInitialise" << endl;
f8513317
LOK
216 return NULL;
217 }
218
caca2d81 219 return (CEC::ICECAdapter*) LibCecInitialise(configuration);
f8513317
LOK
220}
221
53024cc1
LOK
222/*!
223 * @brief Destroy an instance of libCEC.
224 * @param device The instance to destroy.
225 */
acec5f48
LOK
226void UnloadLibCec(CEC::ICECAdapter *device)
227{
228 typedef void* _DestroyLibCec(CEC::ICECAdapter *);
229 _DestroyLibCec *DestroyLibCec = (_DestroyLibCec*) dlsym(g_libCEC, "CECDestroy");
230 if (DestroyLibCec)
231 DestroyLibCec(device);
232
233 dlclose(g_libCEC);
234}
235
a2198e5e
LOK
236/*!
237 * @brief Start the bootloader on the first device that was detected.
238 * @param strLib The name of and/or path to libCEC
239 * @return True when the command was sent, false otherwise.
240 */
241bool LibCecBootloader(const char *strLib = NULL)
242{
243 if (!g_libCEC)
244 {
245#if defined(__APPLE__)
246 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
247#else
248 g_libCEC = dlopen(strLib ? strLib : "libcec.so", RTLD_LAZY);
249#endif
250 if (!g_libCEC)
251 {
252#if defined(__APPLE__)
253 cout << "cannot find " << (strLib ? strLib : "libcec.dylib") << dlerror() << endl;
254#else
255 cout << "cannot find " << (strLib ? strLib : "libcec.so") << dlerror() << endl;
256#endif
257 return NULL;
258 }
259 }
260
261 typedef bool _LibCecBootloader(void);
262 _LibCecBootloader* LibCecBootloader = (_LibCecBootloader*) dlsym(g_libCEC, "CECStartBootloader");
263 if (!LibCecBootloader)
264 {
265 cout << "cannot find CECStartBootloader" << endl;
266 return NULL;
267 }
268
269 bool bReturn = LibCecBootloader();
270 UnloadLibCec((CEC::ICECAdapter*)g_libCEC);
271 return bReturn;
272}
273
acec5f48
LOK
274#endif
275
276#endif /* CECLOADER_H_ */