49efbd747c5cd8b537a3a704218ef3f5e5beb202
[deb_libcec.git] / include / cecloader.h
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33 #ifndef CECLOADER_H_
34 #define CECLOADER_H_
35
36 #if defined(_WIN32) || defined(_WIN64)
37 #include <windows.h>
38 #include <conio.h>
39
40 HINSTANCE g_libCEC = NULL;
41
42 /*!
43 * @deprecated Please use LibCecInit() instead
44 */
45 CEC::ICECAdapter *LoadLibCec(const char *strName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, uint16_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS, const char *strLib = NULL)
46 {
47 if (!g_libCEC)
48 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.dll");
49 if (!g_libCEC)
50 return NULL;
51
52 typedef void* (__cdecl*_CreateLibCec)(const char *, uint8_t, uint16_t);
53 _CreateLibCec CreateLibCec;
54 CreateLibCec = (_CreateLibCec) (GetProcAddress(g_libCEC, "CECCreate"));
55 if (!CreateLibCec)
56 return NULL;
57 return static_cast< CEC::ICECAdapter* > (CreateLibCec(strName, (uint8_t) iLogicalAddress, iPhysicalAddress));
58 }
59
60 /*!
61 * @brief Create a new libCEC instance.
62 * @param strDeviceName The name of the primary device to pass to other CEC devices.
63 * @param types The list of device types to register on the bus.
64 * @param strLib The name of and/or path to libCEC
65 * @return An instance of libCEC or NULL when it failed to load.
66 */
67 CEC::ICECAdapter *LibCecInit(const char *strDeviceName, CEC::cec_device_type_list types, const char *strLib = NULL)
68 {
69 if (!g_libCEC)
70 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.dll");
71 if (!g_libCEC)
72 return NULL;
73
74 typedef void* (__cdecl*_LibCecInit)(const char *, CEC::cec_device_type_list);
75 _LibCecInit LibCecInit;
76 LibCecInit = (_LibCecInit) (GetProcAddress(g_libCEC, "CECInit"));
77 if (!LibCecInit)
78 return NULL;
79 return static_cast< CEC::ICECAdapter* > (LibCecInit(strDeviceName, types));
80 }
81
82 /*!
83 * @brief Destroy an instance of libCEC.
84 * @param device The instance to destroy.
85 */
86 void UnloadLibCec(CEC::ICECAdapter *device)
87 {
88 typedef void (__cdecl*_DestroyLibCec)(void * device);
89 _DestroyLibCec DestroyLibCec;
90 DestroyLibCec = (_DestroyLibCec) (GetProcAddress(g_libCEC, "CECDestroy"));
91 if (DestroyLibCec)
92 DestroyLibCec(device);
93
94 FreeLibrary(g_libCEC);
95 g_libCEC = NULL;
96 }
97
98 #else
99
100 #include <dlfcn.h>
101
102 void *g_libCEC = NULL;
103
104 /*!
105 * @deprecated Please use LibCecInit() instead
106 */
107 CEC::ICECAdapter *LoadLibCec(const char *strName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, uint16_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS, const char *strLib = NULL)
108 {
109 if (!g_libCEC)
110 {
111 #if defined(__APPLE__)
112 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
113 #else
114 g_libCEC = dlopen(strLib ? strLib : "libcec.so", RTLD_LAZY);
115 #endif
116 if (!g_libCEC)
117 {
118 #if defined(__APPLE__)
119 cout << "cannot find " << (strLib ? strLib : "libcec.dylib") << dlerror() << endl;
120 #else
121 cout << "cannot find " << (strLib ? strLib : "libcec.so") << dlerror() << endl;
122 #endif
123 return NULL;
124 }
125 }
126
127 typedef void* _CreateLibCec(const char *, uint8_t, uint16_t);
128 _CreateLibCec* CreateLibCec = (_CreateLibCec*) dlsym(g_libCEC, "CECCreate");
129 if (!CreateLibCec)
130 {
131 cout << "cannot find CECCreate" << endl;
132 return NULL;
133 }
134
135 return (CEC::ICECAdapter*) CreateLibCec(strName, iLogicalAddress, iPhysicalAddress);
136 }
137
138 /*!
139 * @brief Create a new libCEC instance.
140 * @param strDeviceName The name of the primary device to pass to other CEC devices.
141 * @param types The list of device types to register on the bus.
142 * @param strLib The name of and/or path to libCEC
143 * @return An instance of libCEC or NULL when it failed to load.
144 */
145 CEC::ICECAdapter *LibCecInit(const char *strDeviceName, CEC::cec_device_type_list types, const char *strLib = NULL)
146 {
147 if (!g_libCEC)
148 {
149 #if defined(__APPLE__)
150 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
151 #else
152 g_libCEC = dlopen(strLib ? strLib : "libcec.so", RTLD_LAZY);
153 #endif
154 if (!g_libCEC)
155 {
156 #if defined(__APPLE__)
157 cout << "cannot find " << (strLib ? strLib : "libcec.dylib") << dlerror() << endl;
158 #else
159 cout << "cannot find " << (strLib ? strLib : "libcec.so") << dlerror() << endl;
160 #endif
161 return NULL;
162 }
163 }
164
165 typedef void* _LibCecInit(const char *, CEC::cec_device_type_list);
166 _LibCecInit* LibCecInit = (_LibCecInit*) dlsym(g_libCEC, "CECInit");
167 if (!LibCecInit)
168 {
169 cout << "cannot find CECInit" << endl;
170 return NULL;
171 }
172
173 return (CEC::ICECAdapter*) LibCecInit(strDeviceName, types);
174 }
175
176 /*!
177 * @brief Destroy an instance of libCEC.
178 * @param device The instance to destroy.
179 */
180 void UnloadLibCec(CEC::ICECAdapter *device)
181 {
182 typedef void* _DestroyLibCec(CEC::ICECAdapter *);
183 _DestroyLibCec *DestroyLibCec = (_DestroyLibCec*) dlsym(g_libCEC, "CECDestroy");
184 if (DestroyLibCec)
185 DestroyLibCec(device);
186
187 dlclose(g_libCEC);
188 }
189
190 #endif
191
192 #endif /* CECLOADER_H_ */