d6154c36cfe09ddb733b9d37bd08bd0dca4cc0aa
[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 CecCallbackMethods::EnableCallbacks(config->Callbacks);
49 if (!InitialiseLibCec(config))
50 throw gcnew Exception("Could not initialise LibCecSharp");
51 }
52
53 LibCecSharp(String ^ strDeviceName, CecDeviceTypeList ^ deviceTypes)
54 {
55 LibCECConfiguration ^config = gcnew LibCECConfiguration();
56 config->SetCallbacks(this);
57 config->DeviceName = strDeviceName;
58 config->DeviceTypes = deviceTypes;
59 if (!InitialiseLibCec(config))
60 throw gcnew Exception("Could not initialise LibCecSharp");
61 }
62
63 ~LibCecSharp(void)
64 {
65 Close();
66 m_libCec = NULL;
67 }
68
69 private:
70 !LibCecSharp(void)
71 {
72 Close();
73 m_libCec = NULL;
74 }
75
76 bool InitialiseLibCec(LibCECConfiguration ^config)
77 {
78 marshal_context ^ context = gcnew marshal_context();
79 libcec_configuration libCecConfig;
80 ConvertConfiguration(context, config, libCecConfig);
81
82 m_libCec = (ICECAdapter *) CECInitialise(&libCecConfig);
83
84 delete context;
85 return m_libCec != NULL;
86 }
87
88 void ConvertConfiguration(marshal_context ^context, LibCECConfiguration ^netConfig, CEC::libcec_configuration &config)
89 {
90 config.Clear();
91
92 _snprintf_s(config.strDeviceName, 13, context->marshal_as<const char*>(netConfig->DeviceName));
93 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
94 config.deviceTypes.types[iPtr] = (cec_device_type)netConfig->DeviceTypes->Types[iPtr];
95
96 config.bAutodetectAddress = netConfig->AutodetectAddress ? 1 : 0;
97 config.iPhysicalAddress = netConfig->PhysicalAddress;
98 config.baseDevice = (cec_logical_address)netConfig->BaseDevice;
99 config.iHDMIPort = netConfig->HDMIPort;
100 config.clientVersion = (cec_client_version)netConfig->ClientVersion;
101 config.bGetSettingsFromROM = netConfig->GetSettingsFromROM ? 1 : 0;
102 config.bActivateSource = netConfig->ActivateSource ? 1 : 0;
103 config.tvVendor = (cec_vendor_id)netConfig->TvVendor;
104 config.wakeDevices.Clear();
105 for (int iPtr = 0; iPtr < 16; iPtr++)
106 {
107 if (netConfig->WakeDevices->IsSet((CecLogicalAddress)iPtr))
108 config.wakeDevices.Set((cec_logical_address)iPtr);
109 }
110 config.powerOffDevices.Clear();
111 for (int iPtr = 0; iPtr < 16; iPtr++)
112 {
113 if (netConfig->PowerOffDevices->IsSet((CecLogicalAddress)iPtr))
114 config.powerOffDevices.Set((cec_logical_address)iPtr);
115 }
116 config.bPowerOffScreensaver = netConfig->PowerOffScreensaver ? 1 : 0;
117 config.bPowerOffOnStandby = netConfig->PowerOffOnStandby ? 1 : 0;
118 config.callbacks = &g_cecCallbacks;
119 }
120
121 public:
122 array<CecAdapter ^> ^ FindAdapters(String ^ path)
123 {
124 cec_adapter *devices = new cec_adapter[10];
125
126 marshal_context ^ context = gcnew marshal_context();
127 const char* strPathC = path->Length > 0 ? context->marshal_as<const char*>(path) : NULL;
128
129 uint8_t iDevicesFound = m_libCec->FindAdapters(devices, 10, NULL);
130
131 array<CecAdapter ^> ^ adapters = gcnew array<CecAdapter ^>(iDevicesFound);
132 for (unsigned int iPtr = 0; iPtr < iDevicesFound; iPtr++)
133 adapters[iPtr] = gcnew CecAdapter(gcnew String(devices[iPtr].path), gcnew String(devices[iPtr].comm));
134
135 delete devices;
136 delete context;
137 return adapters;
138 }
139
140 bool Open(String ^ strPort, int iTimeoutMs)
141 {
142 marshal_context ^ context = gcnew marshal_context();
143 const char* strPortC = context->marshal_as<const char*>(strPort);
144 bool bReturn = m_libCec->Open(strPortC, iTimeoutMs);
145 delete context;
146 return bReturn;
147 }
148
149 void Close(void)
150 {
151 DisableCallbacks();
152 m_libCec->Close();
153 }
154
155 virtual void DisableCallbacks(void) override
156 {
157 // delete the callbacks, since these might already have been destroyed in .NET
158 CecCallbackMethods::DisableCallbacks();
159 if (m_libCec)
160 m_libCec->EnableCallbacks(NULL, NULL);
161 }
162
163 virtual bool EnableCallbacks(CecCallbackMethods ^ callbacks) override
164 {
165 if (m_libCec && CecCallbackMethods::EnableCallbacks(callbacks))
166 return m_libCec->EnableCallbacks(NULL, &g_cecCallbacks);
167
168 return false;
169 }
170
171 bool PingAdapter(void)
172 {
173 return m_libCec->PingAdapter();
174 }
175
176 bool StartBootloader(void)
177 {
178 return m_libCec->StartBootloader();
179 }
180
181 int GetMinLibVersion(void)
182 {
183 return m_libCec->GetMinLibVersion();
184 }
185
186 int GetLibVersionMajor(void)
187 {
188 return m_libCec->GetLibVersionMajor();
189 }
190
191 int GetLibVersionMinor(void)
192 {
193 return m_libCec->GetLibVersionMinor();
194 }
195
196 CecLogMessage ^ GetNextLogMessage(void)
197 {
198 cec_log_message msg;
199 if (m_libCec->GetNextLogMessage(&msg))
200 {
201 return gcnew CecLogMessage(gcnew String(msg.message), (CecLogLevel)msg.level, msg.time);
202 }
203
204 return gcnew CecLogMessage();
205 }
206
207 CecKeypress ^ GetNextKeypress(void)
208 {
209 cec_keypress key;
210 if (m_libCec->GetNextKeypress(&key))
211 {
212 return gcnew CecKeypress(key.keycode, key.duration);
213 }
214
215 return gcnew CecKeypress();
216 }
217
218 CecCommand ^ GetNextCommand(void)
219 {
220 cec_command command;
221 if (m_libCec->GetNextCommand(&command))
222 {
223 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);
224 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
225 retVal->Parameters->PushBack(command.parameters[iPtr]);
226 return retVal;
227 }
228
229 return gcnew CecCommand();
230 }
231
232 bool Transmit(CecCommand ^ command)
233 {
234 cec_command ccommand;
235 cec_command::Format(ccommand, (cec_logical_address)command->Initiator, (cec_logical_address)command->Destination, (cec_opcode)command->Opcode);
236 ccommand.transmit_timeout = command->TransmitTimeout;
237 ccommand.eom = command->Eom;
238 ccommand.ack = command->Ack;
239 for (unsigned int iPtr = 0; iPtr < command->Parameters->Size; iPtr++)
240 ccommand.parameters.PushBack(command->Parameters->Data[iPtr]);
241
242 return m_libCec->Transmit(ccommand);
243 }
244
245 bool SetLogicalAddress(CecLogicalAddress logicalAddress)
246 {
247 return m_libCec->SetLogicalAddress((cec_logical_address) logicalAddress);
248 }
249
250 bool SetPhysicalAddress(uint16_t physicalAddress)
251 {
252 return m_libCec->SetPhysicalAddress(physicalAddress);
253 }
254
255 bool PowerOnDevices(CecLogicalAddress logicalAddress)
256 {
257 return m_libCec->PowerOnDevices((cec_logical_address) logicalAddress);
258 }
259
260 bool StandbyDevices(CecLogicalAddress logicalAddress)
261 {
262 return m_libCec->StandbyDevices((cec_logical_address) logicalAddress);
263 }
264
265 bool PollDevice(CecLogicalAddress logicalAddress)
266 {
267 return m_libCec->PollDevice((cec_logical_address) logicalAddress);
268 }
269
270 bool SetActiveSource(CecDeviceType type)
271 {
272 return m_libCec->SetActiveSource((cec_device_type) type);
273 }
274
275 bool SetDeckControlMode(CecDeckControlMode mode, bool sendUpdate)
276 {
277 return m_libCec->SetDeckControlMode((cec_deck_control_mode) mode, sendUpdate);
278 }
279
280 bool SetDeckInfo(CecDeckInfo info, bool sendUpdate)
281 {
282 return m_libCec->SetDeckInfo((cec_deck_info) info, sendUpdate);
283 }
284
285 bool SetInactiveView(void)
286 {
287 return m_libCec->SetInactiveView();
288 }
289
290 bool SetMenuState(CecMenuState state, bool sendUpdate)
291 {
292 return m_libCec->SetMenuState((cec_menu_state) state, sendUpdate);
293 }
294
295 bool SetOSDString(CecLogicalAddress logicalAddress, CecDisplayControl duration, String ^ message)
296 {
297 marshal_context ^ context = gcnew marshal_context();
298 const char* strMessageC = context->marshal_as<const char*>(message);
299
300 bool bReturn = m_libCec->SetOSDString((cec_logical_address) logicalAddress, (cec_display_control) duration, strMessageC);
301
302 delete context;
303 return bReturn;
304 }
305
306 bool SwitchMonitoring(bool enable)
307 {
308 return m_libCec->SwitchMonitoring(enable);
309 }
310
311 CecVersion GetDeviceCecVersion(CecLogicalAddress logicalAddress)
312 {
313 return (CecVersion) m_libCec->GetDeviceCecVersion((cec_logical_address) logicalAddress);
314 }
315
316 String ^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress)
317 {
318 cec_menu_language lang;
319 if (m_libCec->GetDeviceMenuLanguage((cec_logical_address) logicalAddress, &lang))
320 {
321 return gcnew String(lang.language);
322 }
323
324 return gcnew String("");
325 }
326
327 CecVendorId GetDeviceVendorId(CecLogicalAddress logicalAddress)
328 {
329 return (CecVendorId)m_libCec->GetDeviceVendorId((cec_logical_address) logicalAddress);
330 }
331
332 CecPowerStatus GetDevicePowerStatus(CecLogicalAddress logicalAddress)
333 {
334 return (CecPowerStatus) m_libCec->GetDevicePowerStatus((cec_logical_address) logicalAddress);
335 }
336
337 void RescanActiveDevices(void)
338 {
339 m_libCec->RescanActiveDevices();
340 }
341
342 CecLogicalAddresses ^ GetActiveDevices(void)
343 {
344 CecLogicalAddresses ^ retVal = gcnew CecLogicalAddresses();
345 unsigned int iDevices = 0;
346
347 cec_logical_addresses activeDevices = m_libCec->GetActiveDevices();
348
349 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
350 if (activeDevices[iPtr])
351 retVal->Addresses[iDevices++] = (CecLogicalAddress)iPtr;
352
353 return retVal;
354 }
355
356 bool IsActiveDevice(CecLogicalAddress logicalAddress)
357 {
358 return m_libCec->IsActiveDevice((cec_logical_address)logicalAddress);
359 }
360
361 bool IsActiveDeviceType(CecDeviceType type)
362 {
363 return m_libCec->IsActiveDeviceType((cec_device_type)type);
364 }
365
366 bool SetHDMIPort(CecLogicalAddress address, uint8_t port)
367 {
368 return m_libCec->SetHDMIPort((cec_logical_address)address, port);
369 }
370
371 uint8_t VolumeUp(bool wait)
372 {
373 return m_libCec->VolumeUp(wait);
374 }
375
376 uint8_t VolumeDown(bool wait)
377 {
378 return m_libCec->VolumeDown(wait);
379 }
380
381 uint8_t MuteAudio(bool wait)
382 {
383 return m_libCec->MuteAudio(wait);
384 }
385
386 bool SendKeypress(CecLogicalAddress destination, CecUserControlCode key, bool wait)
387 {
388 return m_libCec->SendKeypress((cec_logical_address)destination, (cec_user_control_code)key, wait);
389 }
390
391 bool SendKeyRelease(CecLogicalAddress destination, bool wait)
392 {
393 return m_libCec->SendKeyRelease((cec_logical_address)destination, wait);
394 }
395
396 String ^ GetDeviceOSDName(CecLogicalAddress logicalAddress)
397 {
398 cec_osd_name osd = m_libCec->GetDeviceOSDName((cec_logical_address) logicalAddress);
399 return gcnew String(osd.name);
400 }
401
402 CecLogicalAddress GetActiveSource()
403 {
404 return (CecLogicalAddress)m_libCec->GetActiveSource();
405 }
406
407 bool IsActiveSource(CecLogicalAddress logicalAddress)
408 {
409 return m_libCec->IsActiveSource((cec_logical_address)logicalAddress);
410 }
411
412 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress)
413 {
414 return m_libCec->GetDevicePhysicalAddress((cec_logical_address)iAddress);
415 }
416
417 bool SetStreamPath(CecLogicalAddress iAddress)
418 {
419 return m_libCec->SetStreamPath((cec_logical_address)iAddress);
420 }
421
422 bool SetStreamPath(uint16_t iPhysicalAddress)
423 {
424 return m_libCec->SetStreamPath(iPhysicalAddress);
425 }
426
427 CecLogicalAddresses ^GetLogicalAddresses(void)
428 {
429 CecLogicalAddresses ^addr = gcnew CecLogicalAddresses();
430 cec_logical_addresses libAddr = m_libCec->GetLogicalAddresses();
431 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
432 addr->Addresses[iPtr] = (CecLogicalAddress)libAddr.addresses[iPtr];
433 addr->Primary = (CecLogicalAddress)libAddr.primary;
434 return addr;
435 }
436
437 bool GetCurrentConfiguration(LibCECConfiguration ^configuration)
438 {
439 libcec_configuration config;
440 config.Clear();
441
442 if (m_libCec->GetCurrentConfiguration(&config))
443 {
444 configuration->Update(config);
445 return true;
446 }
447 return false;
448 }
449
450 bool CanPersistConfiguration(void)
451 {
452 return m_libCec->CanPersistConfiguration();
453 }
454
455 bool PersistConfiguration(LibCECConfiguration ^configuration)
456 {
457 marshal_context ^ context = gcnew marshal_context();
458 libcec_configuration config;
459 ConvertConfiguration(context, configuration, config);
460
461 bool bReturn = m_libCec->PersistConfiguration(&config);
462
463 delete context;
464 return bReturn;
465 }
466
467 bool SetConfiguration(LibCECConfiguration ^configuration)
468 {
469 marshal_context ^ context = gcnew marshal_context();
470 libcec_configuration config;
471 ConvertConfiguration(context, configuration, config);
472
473 bool bReturn = m_libCec->SetConfiguration(&config);
474
475 delete context;
476 return bReturn;
477 }
478
479 String ^ ToString(CecLogicalAddress iAddress)
480 {
481 const char *retVal = m_libCec->ToString((cec_logical_address)iAddress);
482 return gcnew String(retVal);
483 }
484
485 String ^ ToString(CecVendorId iVendorId)
486 {
487 const char *retVal = m_libCec->ToString((cec_vendor_id)iVendorId);
488 return gcnew String(retVal);
489 }
490
491 String ^ ToString(CecVersion iVersion)
492 {
493 const char *retVal = m_libCec->ToString((cec_version)iVersion);
494 return gcnew String(retVal);
495 }
496
497 String ^ ToString(CecPowerStatus iState)
498 {
499 const char *retVal = m_libCec->ToString((cec_power_status)iState);
500 return gcnew String(retVal);
501 }
502
503 String ^ ToString(CecMenuState iState)
504 {
505 const char *retVal = m_libCec->ToString((cec_menu_state)iState);
506 return gcnew String(retVal);
507 }
508
509 String ^ ToString(CecDeckControlMode iMode)
510 {
511 const char *retVal = m_libCec->ToString((cec_deck_control_mode)iMode);
512 return gcnew String(retVal);
513 }
514
515 String ^ ToString(CecDeckInfo status)
516 {
517 const char *retVal = m_libCec->ToString((cec_deck_info)status);
518 return gcnew String(retVal);
519 }
520
521 String ^ ToString(CecOpcode opcode)
522 {
523 const char *retVal = m_libCec->ToString((cec_opcode)opcode);
524 return gcnew String(retVal);
525 }
526
527 String ^ ToString(CecSystemAudioStatus mode)
528 {
529 const char *retVal = m_libCec->ToString((cec_system_audio_status)mode);
530 return gcnew String(retVal);
531 }
532
533 String ^ ToString(CecAudioStatus status)
534 {
535 const char *retVal = m_libCec->ToString((cec_audio_status)status);
536 return gcnew String(retVal);
537 }
538
539 String ^ ToString(CecClientVersion version)
540 {
541 const char *retVal = m_libCec->ToString((cec_client_version)version);
542 return gcnew String(retVal);
543 }
544
545 String ^ ToString(CecServerVersion version)
546 {
547 const char *retVal = m_libCec->ToString((cec_server_version)version);
548 return gcnew String(retVal);
549 }
550
551 private:
552 ICECAdapter * m_libCec;
553 };
554 }