cec: added the new initialise method to LibCecSharp (as a new constructor)
[deb_libcec.git] / src / LibCecSharp / LibCecSharp.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 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 #include "CecSharpTypes.h"
34 #using <System.dll>
35
36 using namespace System;
37 using namespace System::Runtime::InteropServices;
38 using namespace CEC;
39 using namespace msclr::interop;
40
41 namespace CecSharp
42 {
43 public ref class LibCecSharp : public CecCallbackMethods
44 {
45 public:
46 LibCecSharp(LibCECConfiguration ^config)
47 {
48 m_configuration = config;
49 m_configuration->SetCallbacks(this);
50 if (!InitialiseLibCec())
51 throw gcnew Exception("Could not initialise LibCecSharp");
52 }
53
54 LibCecSharp(String ^ strDeviceName, CecDeviceTypeList ^ deviceTypes)
55 {
56 m_configuration = gcnew LibCECConfiguration();
57 m_configuration->SetCallbacks(this);
58 m_configuration->DeviceName = strDeviceName;
59 m_configuration->DeviceTypes = deviceTypes;
60 if (!InitialiseLibCec())
61 throw gcnew Exception("Could not initialise LibCecSharp");
62 }
63
64 ~LibCecSharp(void)
65 {
66 Close();
67 m_libCec = NULL;
68 }
69
70 private:
71 !LibCecSharp(void)
72 {
73 Close();
74 m_libCec = NULL;
75 }
76
77 bool InitialiseLibCec(void)
78 {
79 marshal_context ^ context = gcnew marshal_context();
80 libcec_configuration config;
81 GetConfiguration(context, config);
82
83 m_libCec = (ICECAdapter *) CECInitialise(&config);
84
85 delete context;
86 return m_libCec != NULL;
87 }
88
89 void GetConfiguration(marshal_context ^context, libcec_configuration &config)
90 {
91 config.Clear();
92
93 _snprintf_s(config.strDeviceName, 13, context->marshal_as<const char*>(m_configuration->DeviceName));
94 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
95 config.deviceTypes.types[iPtr] = (cec_device_type)m_configuration->DeviceTypes->Types[iPtr];
96
97 config.iPhysicalAddress = m_configuration->PhysicalAddress;
98 config.baseDevice = (cec_logical_address)m_configuration->BaseDevice;
99 config.iHDMIPort = m_configuration->HDMIPort;
100 config.clientVersion = (cec_client_version)m_configuration->ClientVersion;
101 config.bGetSettingsFromROM = m_configuration->GetSettingsFromROM;
102 config.bPowerOnStartup = m_configuration->PowerOnStartup;
103 config.bPowerOffShutdown = m_configuration->PowerOffShutdown;
104 config.bPowerOffScreensaver = m_configuration->PowerOffScreensaver;
105 config.bPowerOffOnStandby = m_configuration->PowerOffOnStandby;
106 config.callbacks = &g_cecCallbacks;
107 }
108
109 public:
110 array<CecAdapter ^> ^ FindAdapters(String ^ path)
111 {
112 cec_adapter *devices = new cec_adapter[10];
113
114 marshal_context ^ context = gcnew marshal_context();
115 const char* strPathC = path->Length > 0 ? context->marshal_as<const char*>(path) : NULL;
116
117 uint8_t iDevicesFound = m_libCec->FindAdapters(devices, 10, NULL);
118
119 array<CecAdapter ^> ^ adapters = gcnew array<CecAdapter ^>(iDevicesFound);
120 for (unsigned int iPtr = 0; iPtr < iDevicesFound; iPtr++)
121 adapters[iPtr] = gcnew CecAdapter(gcnew String(devices[iPtr].path), gcnew String(devices[iPtr].comm));
122
123 delete devices;
124 delete context;
125 return adapters;
126 }
127
128 bool Open(String ^ strPort, int iTimeoutMs)
129 {
130 marshal_context ^ context = gcnew marshal_context();
131 const char* strPortC = context->marshal_as<const char*>(strPort);
132 bool bReturn = m_libCec->Open(strPortC, iTimeoutMs);
133 delete context;
134 return bReturn;
135 }
136
137 void Close(void)
138 {
139 m_libCec->Close();
140 }
141
142 virtual bool EnableCallbacks(CecCallbackMethods ^ callbacks) override
143 {
144 if (m_libCec && CecCallbackMethods::EnableCallbacks(callbacks))
145 return m_libCec->EnableCallbacks(NULL, &g_cecCallbacks);
146
147 return false;
148 }
149
150 bool PingAdapter(void)
151 {
152 return m_libCec->PingAdapter();
153 }
154
155 bool StartBootloader(void)
156 {
157 return m_libCec->StartBootloader();
158 }
159
160 int GetMinLibVersion(void)
161 {
162 return m_libCec->GetMinLibVersion();
163 }
164
165 int GetLibVersionMajor(void)
166 {
167 return m_libCec->GetLibVersionMajor();
168 }
169
170 int GetLibVersionMinor(void)
171 {
172 return m_libCec->GetLibVersionMinor();
173 }
174
175 CecLogMessage ^ GetNextLogMessage(void)
176 {
177 cec_log_message msg;
178 if (m_libCec->GetNextLogMessage(&msg))
179 {
180 return gcnew CecLogMessage(gcnew String(msg.message), (CecLogLevel)msg.level, msg.time);
181 }
182
183 return gcnew CecLogMessage();
184 }
185
186 CecKeypress ^ GetNextKeypress(void)
187 {
188 cec_keypress key;
189 if (m_libCec->GetNextKeypress(&key))
190 {
191 return gcnew CecKeypress(key.keycode, key.duration);
192 }
193
194 return gcnew CecKeypress();
195 }
196
197 CecCommand ^ GetNextCommand(void)
198 {
199 cec_command command;
200 if (m_libCec->GetNextCommand(&command))
201 {
202 CecCommand ^ retVal = gcnew CecCommand((CecLogicalAddress)command.initiator, (CecLogicalAddress)command.destination, command.ack == 1 ? true : false, command.eom == 1 ? true : false, (CecOpcode)command.opcode, command.transmit_timeout);
203 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
204 retVal->Parameters->PushBack(command.parameters[iPtr]);
205 return retVal;
206 }
207
208 return gcnew CecCommand();
209 }
210
211 bool Transmit(CecCommand ^ command)
212 {
213 cec_command ccommand;
214 cec_command::Format(ccommand, (cec_logical_address)command->Initiator, (cec_logical_address)command->Destination, (cec_opcode)command->Opcode);
215 ccommand.transmit_timeout = command->TransmitTimeout;
216 ccommand.eom = command->Eom;
217 ccommand.ack = command->Ack;
218 for (unsigned int iPtr = 0; iPtr < command->Parameters->Size; iPtr++)
219 ccommand.parameters.PushBack(command->Parameters->Data[iPtr]);
220
221 return m_libCec->Transmit(ccommand);
222 }
223
224 bool SetLogicalAddress(CecLogicalAddress logicalAddress)
225 {
226 return m_libCec->SetLogicalAddress((cec_logical_address) logicalAddress);
227 }
228
229 bool SetPhysicalAddress(int16_t physicalAddress)
230 {
231 return m_libCec->SetPhysicalAddress(physicalAddress);
232 }
233
234 bool PowerOnDevices(CecLogicalAddress logicalAddress)
235 {
236 return m_libCec->PowerOnDevices((cec_logical_address) logicalAddress);
237 }
238
239 bool StandbyDevices(CecLogicalAddress logicalAddress)
240 {
241 return m_libCec->StandbyDevices((cec_logical_address) logicalAddress);
242 }
243
244 bool PollDevice(CecLogicalAddress logicalAddress)
245 {
246 return m_libCec->PollDevice((cec_logical_address) logicalAddress);
247 }
248
249 bool SetActiveSource(CecDeviceType type)
250 {
251 return m_libCec->SetActiveSource((cec_device_type) type);
252 }
253
254 bool SetDeckControlMode(CecDeckControlMode mode, bool sendUpdate)
255 {
256 return m_libCec->SetDeckControlMode((cec_deck_control_mode) mode, sendUpdate);
257 }
258
259 bool SetDeckInfo(CecDeckInfo info, bool sendUpdate)
260 {
261 return m_libCec->SetDeckInfo((cec_deck_info) info, sendUpdate);
262 }
263
264 bool SetInactiveView(void)
265 {
266 return m_libCec->SetInactiveView();
267 }
268
269 bool SetMenuState(CecMenuState state, bool sendUpdate)
270 {
271 return m_libCec->SetMenuState((cec_menu_state) state, sendUpdate);
272 }
273
274 bool SetOSDString(CecLogicalAddress logicalAddress, CecDisplayControl duration, String ^ message)
275 {
276 marshal_context ^ context = gcnew marshal_context();
277 const char* strMessageC = context->marshal_as<const char*>(message);
278
279 bool bReturn = m_libCec->SetOSDString((cec_logical_address) logicalAddress, (cec_display_control) duration, strMessageC);
280
281 delete context;
282 return bReturn;
283 }
284
285 bool SwitchMonitoring(bool enable)
286 {
287 return m_libCec->SwitchMonitoring(enable);
288 }
289
290 CecVersion GetDeviceCecVersion(CecLogicalAddress logicalAddress)
291 {
292 return (CecVersion) m_libCec->GetDeviceCecVersion((cec_logical_address) logicalAddress);
293 }
294
295 String ^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress)
296 {
297 cec_menu_language lang;
298 if (m_libCec->GetDeviceMenuLanguage((cec_logical_address) logicalAddress, &lang))
299 {
300 return gcnew String(lang.language);
301 }
302
303 return gcnew String("");
304 }
305
306 CecVendorId GetDeviceVendorId(CecLogicalAddress logicalAddress)
307 {
308 return (CecVendorId)m_libCec->GetDeviceVendorId((cec_logical_address) logicalAddress);
309 }
310
311 CecPowerStatus GetDevicePowerStatus(CecLogicalAddress logicalAddress)
312 {
313 return (CecPowerStatus) m_libCec->GetDevicePowerStatus((cec_logical_address) logicalAddress);
314 }
315
316 CecLogicalAddresses ^ GetActiveDevices(void)
317 {
318 CecLogicalAddresses ^ retVal = gcnew CecLogicalAddresses();
319 unsigned int iDevices = 0;
320
321 cec_logical_addresses activeDevices = m_libCec->GetActiveDevices();
322
323 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
324 if (activeDevices[iPtr])
325 retVal->Addresses[iDevices++] = (CecLogicalAddress)iPtr;
326
327 return retVal;
328 }
329
330 bool IsActiveDevice(CecLogicalAddress logicalAddress)
331 {
332 return m_libCec->IsActiveDevice((cec_logical_address)logicalAddress);
333 }
334
335 bool IsActiveDeviceType(CecDeviceType type)
336 {
337 return m_libCec->IsActiveDeviceType((cec_device_type)type);
338 }
339
340 bool SetHDMIPort(CecLogicalAddress address, uint8_t port)
341 {
342 return m_libCec->SetHDMIPort((cec_logical_address)address, port);
343 }
344
345 uint8_t VolumeUp(bool wait)
346 {
347 return m_libCec->VolumeUp(wait);
348 }
349
350 uint8_t VolumeDown(bool wait)
351 {
352 return m_libCec->VolumeDown(wait);
353 }
354
355 uint8_t MuteAudio(bool wait)
356 {
357 return m_libCec->MuteAudio(wait);
358 }
359
360 bool SendKeypress(CecLogicalAddress destination, CecUserControlCode key, bool wait)
361 {
362 return m_libCec->SendKeypress((cec_logical_address)destination, (cec_user_control_code)key, wait);
363 }
364
365 bool SendKeyRelease(CecLogicalAddress destination, bool wait)
366 {
367 return m_libCec->SendKeyRelease((cec_logical_address)destination, wait);
368 }
369
370 String ^ GetDeviceOSDName(CecLogicalAddress logicalAddress)
371 {
372 cec_osd_name osd = m_libCec->GetDeviceOSDName((cec_logical_address) logicalAddress);
373 return gcnew String(osd.name);
374 }
375
376 CecLogicalAddress GetActiveSource()
377 {
378 return (CecLogicalAddress)m_libCec->GetActiveSource();
379 }
380
381 bool IsActiveSource(CecLogicalAddress logicalAddress)
382 {
383 return m_libCec->IsActiveSource((cec_logical_address)logicalAddress);
384 }
385
386 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress)
387 {
388 return m_libCec->GetDevicePhysicalAddress((cec_logical_address)iAddress);
389 }
390
391 String ^ ToString(CecLogicalAddress iAddress)
392 {
393 const char *retVal = m_libCec->ToString((cec_logical_address)iAddress);
394 return gcnew String(retVal);
395 }
396
397 String ^ ToString(CecVendorId iVendorId)
398 {
399 const char *retVal = m_libCec->ToString((cec_vendor_id)iVendorId);
400 return gcnew String(retVal);
401 }
402
403 String ^ ToString(CecVersion iVersion)
404 {
405 const char *retVal = m_libCec->ToString((cec_version)iVersion);
406 return gcnew String(retVal);
407 }
408
409 String ^ ToString(CecPowerStatus iState)
410 {
411 const char *retVal = m_libCec->ToString((cec_power_status)iState);
412 return gcnew String(retVal);
413 }
414
415 String ^ ToString(CecMenuState iState)
416 {
417 const char *retVal = m_libCec->ToString((cec_menu_state)iState);
418 return gcnew String(retVal);
419 }
420
421 String ^ ToString(CecDeckControlMode iMode)
422 {
423 const char *retVal = m_libCec->ToString((cec_deck_control_mode)iMode);
424 return gcnew String(retVal);
425 }
426
427 String ^ ToString(CecDeckInfo status)
428 {
429 const char *retVal = m_libCec->ToString((cec_deck_info)status);
430 return gcnew String(retVal);
431 }
432
433 String ^ ToString(CecOpcode opcode)
434 {
435 const char *retVal = m_libCec->ToString((cec_opcode)opcode);
436 return gcnew String(retVal);
437 }
438
439 String ^ ToString(CecSystemAudioStatus mode)
440 {
441 const char *retVal = m_libCec->ToString((cec_system_audio_status)mode);
442 return gcnew String(retVal);
443 }
444
445 String ^ ToString(CecAudioStatus status)
446 {
447 const char *retVal = m_libCec->ToString((cec_audio_status)status);
448 return gcnew String(retVal);
449 }
450
451 private:
452 ICECAdapter * m_libCec;
453 LibCECConfiguration ^m_configuration;
454 };
455 }