cec: refactored threading/locking - added windows native instead of pthread-win32...
[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 *
5 * libCEC(R) is Copyright (C) 2011 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
41HINSTANCE g_libCEC = NULL;
42
53024cc1
LOK
43/*!
44 * @deprecated Please use LibCecInit() instead
45 */
dad7a92e 46CEC::ICECAdapter *LoadLibCec(const char *strName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, uint16_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS, const char *strLib = NULL)
acec5f48 47{
acec5f48 48 if (!g_libCEC)
f00ff009
LOK
49#if defined(_WIN64)
50 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.x64.dll");
51#else
dad7a92e 52 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.dll");
f00ff009 53#endif
acec5f48
LOK
54 if (!g_libCEC)
55 return NULL;
56
dad7a92e
LOK
57 typedef void* (__cdecl*_CreateLibCec)(const char *, uint8_t, uint16_t);
58 _CreateLibCec CreateLibCec;
acec5f48
LOK
59 CreateLibCec = (_CreateLibCec) (GetProcAddress(g_libCEC, "CECCreate"));
60 if (!CreateLibCec)
61 return NULL;
62 return static_cast< CEC::ICECAdapter* > (CreateLibCec(strName, (uint8_t) iLogicalAddress, iPhysicalAddress));
63}
64
53024cc1
LOK
65/*!
66 * @brief Create a new libCEC instance.
67 * @param strDeviceName The name of the primary device to pass to other CEC devices.
68 * @param types The list of device types to register on the bus.
69 * @param strLib The name of and/or path to libCEC
70 * @return An instance of libCEC or NULL when it failed to load.
71 */
d2eadc31
LOK
72CEC::ICECAdapter *LibCecInit(const char *strDeviceName, CEC::cec_device_type_list types, const char *strLib = NULL)
73{
74 if (!g_libCEC)
f00ff009
LOK
75#if defined(_WIN64)
76 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.x64.dll");
77#else
d2eadc31 78 g_libCEC = LoadLibrary(strLib ? strLib : "libcec.dll");
f00ff009 79#endif
d2eadc31
LOK
80 if (!g_libCEC)
81 return NULL;
82
83 typedef void* (__cdecl*_LibCecInit)(const char *, CEC::cec_device_type_list);
84 _LibCecInit LibCecInit;
85 LibCecInit = (_LibCecInit) (GetProcAddress(g_libCEC, "CECInit"));
86 if (!LibCecInit)
87 return NULL;
88 return static_cast< CEC::ICECAdapter* > (LibCecInit(strDeviceName, types));
89}
90
53024cc1
LOK
91/*!
92 * @brief Destroy an instance of libCEC.
93 * @param device The instance to destroy.
94 */
acec5f48
LOK
95void UnloadLibCec(CEC::ICECAdapter *device)
96{
97 typedef void (__cdecl*_DestroyLibCec)(void * device);
98 _DestroyLibCec DestroyLibCec;
99 DestroyLibCec = (_DestroyLibCec) (GetProcAddress(g_libCEC, "CECDestroy"));
100 if (DestroyLibCec)
101 DestroyLibCec(device);
102
103 FreeLibrary(g_libCEC);
104 g_libCEC = NULL;
105}
106
107#else
108
109#include <dlfcn.h>
110
111void *g_libCEC = NULL;
112
53024cc1
LOK
113/*!
114 * @deprecated Please use LibCecInit() instead
115 */
dad7a92e 116CEC::ICECAdapter *LoadLibCec(const char *strName, CEC::cec_logical_address iLogicalAddress = CEC::CECDEVICE_PLAYBACKDEVICE1, uint16_t iPhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS, const char *strLib = NULL)
acec5f48 117{
acec5f48
LOK
118 if (!g_libCEC)
119 {
30a57ee8 120#if defined(__APPLE__)
dad7a92e 121 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
30a57ee8 122#else
dad7a92e 123 g_libCEC = dlopen(strLib ? strLib : "libcec.so", RTLD_LAZY);
30a57ee8 124#endif
acec5f48
LOK
125 if (!g_libCEC)
126 {
30a57ee8 127#if defined(__APPLE__)
e9de9629 128 cout << "cannot find " << (strLib ? strLib : "libcec.dylib") << dlerror() << endl;
30a57ee8 129#else
e9de9629 130 cout << "cannot find " << (strLib ? strLib : "libcec.so") << dlerror() << endl;
30a57ee8 131#endif
acec5f48
LOK
132 return NULL;
133 }
134 }
135
dad7a92e 136 typedef void* _CreateLibCec(const char *, uint8_t, uint16_t);
acec5f48
LOK
137 _CreateLibCec* CreateLibCec = (_CreateLibCec*) dlsym(g_libCEC, "CECCreate");
138 if (!CreateLibCec)
139 {
f8513317 140 cout << "cannot find CECCreate" << endl;
acec5f48
LOK
141 return NULL;
142 }
143
144 return (CEC::ICECAdapter*) CreateLibCec(strName, iLogicalAddress, iPhysicalAddress);
145}
146
53024cc1
LOK
147/*!
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.
153 */
f8513317
LOK
154CEC::ICECAdapter *LibCecInit(const char *strDeviceName, CEC::cec_device_type_list types, const char *strLib = NULL)
155{
156 if (!g_libCEC)
157 {
158#if defined(__APPLE__)
159 g_libCEC = dlopen(strLib ? strLib : "libcec.dylib", RTLD_LAZY);
160#else
161 g_libCEC = dlopen(strLib ? strLib : "libcec.so", RTLD_LAZY);
162#endif
163 if (!g_libCEC)
164 {
165#if defined(__APPLE__)
166 cout << "cannot find " << (strLib ? strLib : "libcec.dylib") << dlerror() << endl;
167#else
168 cout << "cannot find " << (strLib ? strLib : "libcec.so") << dlerror() << endl;
169#endif
170 return NULL;
171 }
172 }
173
174 typedef void* _LibCecInit(const char *, CEC::cec_device_type_list);
175 _LibCecInit* LibCecInit = (_LibCecInit*) dlsym(g_libCEC, "CECInit");
176 if (!LibCecInit)
177 {
178 cout << "cannot find CECInit" << endl;
179 return NULL;
180 }
181
182 return (CEC::ICECAdapter*) LibCecInit(strDeviceName, types);
183}
184
53024cc1
LOK
185/*!
186 * @brief Destroy an instance of libCEC.
187 * @param device The instance to destroy.
188 */
acec5f48
LOK
189void UnloadLibCec(CEC::ICECAdapter *device)
190{
191 typedef void* _DestroyLibCec(CEC::ICECAdapter *);
192 _DestroyLibCec *DestroyLibCec = (_DestroyLibCec*) dlsym(g_libCEC, "CECDestroy");
193 if (DestroyLibCec)
194 DestroyLibCec(device);
195
196 dlclose(g_libCEC);
197}
198
199#endif
200
201#endif /* CECLOADER_H_ */