33a2e3feaf666e56e2212afa202b3c4b58744e28
[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 Destroy an instance of libCEC.
71 * @param device The instance to destroy.
72 */
73 void UnloadLibCec(CEC::ICECAdapter *device)
74 {
75 typedef void (__cdecl*_DestroyLibCec)(void * device);
76 _DestroyLibCec DestroyLibCec;
77 DestroyLibCec = (_DestroyLibCec) (GetProcAddress(g_libCEC, "CECDestroy"));
78 if (DestroyLibCec)
79 DestroyLibCec(device);
80
81 FreeLibrary(g_libCEC);
82 g_libCEC = NULL;
83 }
84
85 #else
86
87 #include <dlfcn.h>
88
89 void *g_libCEC = NULL;
90
91 /*!
92 * @deprecated Please use LibCecInit() instead
93 */
94 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)
95 {
96 if (!g_libCEC)
97 {
98 #if defined(__APPLE__)
99 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
100 #else
101 g_libCEC = dlopen(strLib ? strLib : "libcec.so", RTLD_LAZY);
102 #endif
103 if (!g_libCEC)
104 {
105 #if defined(__APPLE__)
106 cout << "cannot find " << (strLib ? strLib : "libcec.dylib") << dlerror() << endl;
107 #else
108 cout << "cannot find " << (strLib ? strLib : "libcec.so") << dlerror() << endl;
109 #endif
110 return NULL;
111 }
112 }
113
114 typedef void* _CreateLibCec(const char *, uint8_t, uint16_t);
115 _CreateLibCec* CreateLibCec = (_CreateLibCec*) dlsym(g_libCEC, "CECCreate");
116 if (!CreateLibCec)
117 {
118 cout << "cannot find CECCreate" << endl;
119 return NULL;
120 }
121
122 return (CEC::ICECAdapter*) CreateLibCec(strName, iLogicalAddress, iPhysicalAddress);
123 }
124
125 /*!
126 * @brief Create a new libCEC instance.
127 * @param strDeviceName The name of the primary device to pass to other CEC devices.
128 * @param types The list of device types to register on the bus.
129 * @param strLib The name of and/or path to libCEC
130 * @return An instance of libCEC or NULL when it failed to load.
131 */
132 CEC::ICECAdapter *LibCecInit(const char *strDeviceName, CEC::cec_device_type_list types, const char *strLib = NULL)
133 {
134 if (!g_libCEC)
135 {
136 #if defined(__APPLE__)
137 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
138 #else
139 g_libCEC = dlopen(strLib ? strLib : "libcec.so", RTLD_LAZY);
140 #endif
141 if (!g_libCEC)
142 {
143 #if defined(__APPLE__)
144 cout << "cannot find " << (strLib ? strLib : "libcec.dylib") << dlerror() << endl;
145 #else
146 cout << "cannot find " << (strLib ? strLib : "libcec.so") << dlerror() << endl;
147 #endif
148 return NULL;
149 }
150 }
151
152 typedef void* _LibCecInit(const char *, CEC::cec_device_type_list);
153 _LibCecInit* LibCecInit = (_LibCecInit*) dlsym(g_libCEC, "CECInit");
154 if (!LibCecInit)
155 {
156 cout << "cannot find CECInit" << endl;
157 return NULL;
158 }
159
160 return (CEC::ICECAdapter*) LibCecInit(strDeviceName, types);
161 }
162
163 /*!
164 * @brief Destroy an instance of libCEC.
165 * @param device The instance to destroy.
166 */
167 void UnloadLibCec(CEC::ICECAdapter *device)
168 {
169 typedef void* _DestroyLibCec(CEC::ICECAdapter *);
170 _DestroyLibCec *DestroyLibCec = (_DestroyLibCec*) dlsym(g_libCEC, "CECDestroy");
171 if (DestroyLibCec)
172 DestroyLibCec(device);
173
174 dlclose(g_libCEC);
175 }
176
177 #endif
178
179 #endif /* CECLOADER_H_ */