make liblockdev optional
[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
caca2d81
LOK
43/*!
44 * @brief Create a new libCEC instance.
45 * @param configuration The configuration to pass to libCEC
46 * @param strLib The name of and/or path to libCEC
47 * @return An instance of ICECAdapter or NULL on error.
48 */
3efda01a 49CEC::ICECAdapter *LibCecInitialise(CEC::libcec_configuration *configuration, const char *strLib = NULL)
caca2d81
LOK
50{
51 if (!g_libCEC)
52#if defined(_WIN64)
53 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.x64.dll");
54#else
55 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.dll");
56#endif
57 if (!g_libCEC)
58 return NULL;
59
3efda01a 60 typedef void* (__cdecl*_LibCecInitialise)(CEC::libcec_configuration *);
caca2d81
LOK
61 _LibCecInitialise LibCecInitialise;
62 LibCecInitialise = (_LibCecInitialise) (GetProcAddress(g_libCEC, "CECInitialise"));
63 if (!LibCecInitialise)
64 {
65 cout << "cannot find CECInitialise" << endl;
66 return NULL;
67 }
68
69 return static_cast< CEC::ICECAdapter* > (LibCecInitialise(configuration));
70}
71
53024cc1
LOK
72/*!
73 * @brief Destroy an instance of libCEC.
74 * @param device The instance to destroy.
75 */
acec5f48
LOK
76void UnloadLibCec(CEC::ICECAdapter *device)
77{
78 typedef void (__cdecl*_DestroyLibCec)(void * device);
79 _DestroyLibCec DestroyLibCec;
80 DestroyLibCec = (_DestroyLibCec) (GetProcAddress(g_libCEC, "CECDestroy"));
81 if (DestroyLibCec)
82 DestroyLibCec(device);
83
84 FreeLibrary(g_libCEC);
85 g_libCEC = NULL;
86}
87
a2198e5e
LOK
88/*!
89 * @brief Start the bootloader on the first device that was detected.
90 * @param strLib The name of and/or path to libCEC
91 * @return True when the command was sent, false otherwise.
92 */
93bool LibCecBootloader(const char *strLib = NULL)
94{
95 if (!g_libCEC)
96#if defined(_WIN64)
97 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.x64.dll");
98#else
99 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.dll");
100#endif
101 if (!g_libCEC)
102 return NULL;
103
104 typedef bool (__cdecl*_LibCecBootloader)(void);
105 _LibCecBootloader LibCecBootloader;
106 LibCecBootloader = (_LibCecBootloader) (GetProcAddress(g_libCEC, "CECStartBootloader"));
107 if (!LibCecBootloader)
108 return false;
109
110 bool bReturn = LibCecBootloader();
13267208
LOK
111 FreeLibrary(g_libCEC);
112 g_libCEC = NULL;
a2198e5e
LOK
113 return bReturn;
114}
115
acec5f48
LOK
116#else
117
118#include <dlfcn.h>
119
120void *g_libCEC = NULL;
121
53024cc1
LOK
122/*!
123 * @brief Create a new libCEC instance.
caca2d81 124 * @param configuration The configuration to pass to libCEC
39b1216c 125 * @param strLib The name of and/or path to libCEC
caca2d81 126 * @return An instance of ICECAdapter or NULL on error.
53024cc1 127 */
3efda01a 128CEC::ICECAdapter *LibCecInitialise(CEC::libcec_configuration *configuration, const char *strLib = NULL)
f8513317
LOK
129{
130 if (!g_libCEC)
131 {
132#if defined(__APPLE__)
133 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
134#else
a71897f4 135 g_libCEC = dlopen(strLib ? strLib : "libcec.so." CEC_LIB_VERSION_MAJOR_STR, RTLD_LAZY);
f8513317
LOK
136#endif
137 if (!g_libCEC)
138 {
a71897f4 139 cout << dlerror() << endl;
f8513317
LOK
140 return NULL;
141 }
142 }
143
3efda01a 144 typedef void* _LibCecInitialise(CEC::libcec_configuration *);
caca2d81
LOK
145 _LibCecInitialise* LibCecInitialise = (_LibCecInitialise*) dlsym(g_libCEC, "CECInitialise");
146 if (!LibCecInitialise)
f8513317 147 {
caca2d81 148 cout << "cannot find CECInitialise" << endl;
f8513317
LOK
149 return NULL;
150 }
151
caca2d81 152 return (CEC::ICECAdapter*) LibCecInitialise(configuration);
f8513317
LOK
153}
154
53024cc1
LOK
155/*!
156 * @brief Destroy an instance of libCEC.
157 * @param device The instance to destroy.
158 */
acec5f48
LOK
159void UnloadLibCec(CEC::ICECAdapter *device)
160{
161 typedef void* _DestroyLibCec(CEC::ICECAdapter *);
162 _DestroyLibCec *DestroyLibCec = (_DestroyLibCec*) dlsym(g_libCEC, "CECDestroy");
163 if (DestroyLibCec)
164 DestroyLibCec(device);
165
166 dlclose(g_libCEC);
167}
168
a2198e5e
LOK
169/*!
170 * @brief Start the bootloader on the first device that was detected.
171 * @param strLib The name of and/or path to libCEC
172 * @return True when the command was sent, false otherwise.
173 */
174bool LibCecBootloader(const char *strLib = NULL)
175{
176 if (!g_libCEC)
177 {
178#if defined(__APPLE__)
179 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
180#else
a71897f4 181 g_libCEC = dlopen(strLib ? strLib : "libcec.so." CEC_LIB_VERSION_MAJOR_STR, RTLD_LAZY);
a2198e5e
LOK
182#endif
183 if (!g_libCEC)
184 {
a71897f4 185 cout << dlerror() << endl;
a2198e5e
LOK
186 return NULL;
187 }
188 }
189
190 typedef bool _LibCecBootloader(void);
191 _LibCecBootloader* LibCecBootloader = (_LibCecBootloader*) dlsym(g_libCEC, "CECStartBootloader");
192 if (!LibCecBootloader)
193 {
194 cout << "cannot find CECStartBootloader" << endl;
195 return NULL;
196 }
197
198 bool bReturn = LibCecBootloader();
13267208 199 dlclose(g_libCEC);
a2198e5e
LOK
200 return bReturn;
201}
202
acec5f48
LOK
203#endif
204
205#endif /* CECLOADER_H_ */