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