tabs -> spaces
[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-2013 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 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 */
49 CEC::ICECAdapter *LibCecInitialise(CEC::libcec_configuration *configuration, const char *strLib = NULL)
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
60 typedef void* (__cdecl*_LibCecInitialise)(CEC::libcec_configuration *);
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
72 /*!
73 * @brief Destroy an instance of libCEC.
74 * @param device The instance to destroy.
75 */
76 void 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
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 */
93 bool 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();
111 FreeLibrary(g_libCEC);
112 g_libCEC = NULL;
113 return bReturn;
114 }
115
116 #else
117
118 #include <dlfcn.h>
119
120 void *g_libCEC = NULL;
121
122 /*!
123 * @brief Create a new libCEC instance.
124 * @param configuration The configuration to pass to libCEC
125 * @param strLib The name of and/or path to libCEC
126 * @return An instance of ICECAdapter or NULL on error.
127 */
128 CEC::ICECAdapter *LibCecInitialise(CEC::libcec_configuration *configuration, const char *strLib = NULL)
129 {
130 if (!g_libCEC)
131 {
132 #if defined(__APPLE__)
133 g_libCEC = dlopen(strLib ? strLib : "libcec." CEC_LIB_VERSION_MAJOR_STR ".dylib", RTLD_LAZY);
134 #else
135 g_libCEC = dlopen(strLib ? strLib : "libcec.so." CEC_LIB_VERSION_MAJOR_STR, RTLD_LAZY);
136 #endif
137 if (!g_libCEC)
138 {
139 cout << dlerror() << endl;
140 return NULL;
141 }
142 }
143
144 typedef void* _LibCecInitialise(CEC::libcec_configuration *);
145 _LibCecInitialise* LibCecInitialise = (_LibCecInitialise*) dlsym(g_libCEC, "CECInitialise");
146 if (!LibCecInitialise)
147 {
148 cout << "cannot find CECInitialise" << endl;
149 return NULL;
150 }
151
152 return (CEC::ICECAdapter*) LibCecInitialise(configuration);
153 }
154
155 /*!
156 * @brief Destroy an instance of libCEC.
157 * @param device The instance to destroy.
158 */
159 void 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
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 */
174 bool 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
181 g_libCEC = dlopen(strLib ? strLib : "libcec.so." CEC_LIB_VERSION_MAJOR_STR, RTLD_LAZY);
182 #endif
183 if (!g_libCEC)
184 {
185 cout << dlerror() << endl;
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();
199 dlclose(g_libCEC);
200 return bReturn;
201 }
202
203 #endif
204
205 #endif /* CECLOADER_H_ */