1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
/*
* This file is part of the libCEC(R) library.
*
* libCEC(R) is Copyright (C) 2011-2020 Pulse-Eight Limited. All rights reserved.
* libCEC(R) is an original work, containing original code.
*
* libCEC(R) is a trademark of Pulse-Eight Limited.
*
* This program is dual-licensed; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* Alternatively, you can license this library under a commercial license,
* please contact Pulse-Eight Licensing for more information.
*
* For more information contact:
* Pulse-Eight Licensing <license@pulse-eight.com>
* http://www.pulse-eight.com/
* http://www.pulse-eight.net/
*
* Author: Lars Op den Kamp <lars@opdenkamp.eu>
*
*/
#include "CecSharpTypes.h"
#using <System.dll>
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace CEC;
using namespace msclr::interop;
namespace CecSharp
{
/// <summary>
/// Create a LibCecSharp instance and pass the configuration as argument.
/// Then call Open() to open a connection to the adapter. Close() closes the
/// connection.
///
/// libCEC can send commands to other devices on the CEC bus via the methods
/// on this interface, and all commands that libCEC received are sent back
/// to the application via callback methods. The callback methods can be
/// found in CecSharpTypes.h, CecCallbackMethods.
/// </summary>
public ref class LibCecSharp
{
public:
/// <summary>
/// Create a new LibCecSharp instance.
/// </summary>
/// <param name="config">The configuration to pass to libCEC.</param>
LibCecSharp(CecCallbackMethods^ callbacks, LibCECConfiguration^ config)
{
marshal_context^ context = gcnew marshal_context();
libcec_configuration libCecConfig;
m_callbacks = callbacks;
ConvertConfiguration(context, config, libCecConfig);
m_libCec = (ICECAdapter*)CECInitialise(&libCecConfig);
if (!m_libCec)
throw gcnew Exception("Could not initialise LibCecSharp");
config->Update(libCecConfig);
delete context;
}
~LibCecSharp(void)
{
Destroy();
}
/// <summary>
/// Try to find all connected CEC adapters.
/// </summary>
/// <param name="path">The path filter for adapters. Leave empty to return all adapters.</param>
/// <returns>The adapters that were found.</returns>
array<CecAdapter ^> ^ FindAdapters(String ^ path)
{
cec_adapter_descriptor *devices = new cec_adapter_descriptor[10];
marshal_context ^ context = gcnew marshal_context();
const char* strPathC = path->Length > 0 ? context->marshal_as<const char*>(path) : NULL;
uint8_t iDevicesFound = m_libCec->DetectAdapters(devices, 10, NULL, false);
array<CecAdapter ^> ^ adapters = gcnew array<CecAdapter ^>(iDevicesFound);
for (unsigned int iPtr = 0; iPtr < iDevicesFound; iPtr++)
adapters[iPtr] = gcnew CecAdapter(gcnew String(devices[iPtr].strComPath), gcnew String(devices[iPtr].strComName), devices[iPtr].iVendorId, devices[iPtr].iProductId, devices[iPtr].iFirmwareVersion, devices[iPtr].iFirmwareBuildDate, devices[iPtr].iPhysicalAddress);
delete devices;
delete context;
return adapters;
}
/// <summary>
/// Open a connection to the CEC adapter.
/// </summary>
/// <param name="strPort">The COM port of the adapter</param>
/// <param name="iTimeoutMs">Connection timeout in milliseconds</param>
/// <returns>True when a connection was opened, false otherwise.</returns>
bool Open(String ^ strPort, int iTimeoutMs)
{
if (!m_libCec)
return false;
marshal_context ^ context = gcnew marshal_context();
const char* strPortC = context->marshal_as<const char*>(strPort);
bool bReturn = m_libCec->Open(strPortC, iTimeoutMs);
delete context;
return bReturn;
}
/// <summary>
/// Close the connection to the CEC adapter
/// </summary>
void Close(void)
{
if (!!m_libCec)
m_libCec->Close();
}
void EnableCallbacks(void)
{
if (!!m_libCec)
{
#if CEC_LIB_VERSION_MAJOR >= 5
m_libCec->SetCallbacks(GetLibCecCallbacks(), m_callbacks->Get());
#else
m_libCec->EnableCallbacks(m_callbacks->Get(), GetLibCecCallbacks());
#endif
}
}
void DisableCallbacks(void)
{
if (!!m_libCec)
{
#if CEC_LIB_VERSION_MAJOR >= 5
m_libCec->DisableCallbacks();
#else
m_libCec->EnableCallbacks(nullptr, nullptr);
#endif
}
}
/// <summary>
/// Sends a ping command to the adapter, to check if it's responding.
/// </summary>
/// <returns>True when the ping was successful, false otherwise</returns>
bool PingAdapter(void)
{
return !!m_libCec && m_libCec->PingAdapter();
}
/// <summary>
/// Start the bootloader of the CEC adapter. Closes the connection when successful.
/// </summary>
/// <returns>True when the command was sent successfully, false otherwise.</returns>
bool StartBootloader(void)
{
return !!m_libCec && m_libCec->StartBootloader();
}
/// <summary>
/// Transmit a raw CEC command over the CEC line.
/// </summary>
/// <param name="command">The command to transmit</param>
/// <returns>True when the data was sent and acked, false otherwise.</returns>
bool Transmit(CecCommand ^ command)
{
if (!m_libCec) {
return false;
}
cec_command ccommand;
cec_command::Format(ccommand, (cec_logical_address)command->Initiator, (cec_logical_address)command->Destination, (cec_opcode)command->Opcode);
ccommand.transmit_timeout = command->TransmitTimeout;
ccommand.eom = command->Eom;
ccommand.ack = command->Ack;
for (unsigned int iPtr = 0; iPtr < command->Parameters->Size; iPtr++)
ccommand.parameters.PushBack(command->Parameters->Data[iPtr]);
return m_libCec->Transmit(ccommand);
}
/// <summary>
/// Change the logical address on the CEC bus of the CEC adapter. libCEC automatically assigns a logical address, and this method is only available for debugging purposes.
/// </summary>
/// <param name="logicalAddress">The CEC adapter's new logical address.</param>
/// <returns>True when the logical address was set successfully, false otherwise.</returns>
bool SetLogicalAddress(CecLogicalAddress logicalAddress)
{
return !!m_libCec && m_libCec->SetLogicalAddress((cec_logical_address) logicalAddress);
}
/// <summary>
/// Change the physical address (HDMI port) of the CEC adapter. libCEC will try to autodetect the physical address when connecting. If it did, it's set in libcec_configuration.
/// </summary>
/// <param name="physicalAddress">The CEC adapter's new physical address.</param>
/// <returns>True when the physical address was set successfully, false otherwise.</returns>
bool SetPhysicalAddress(uint16_t physicalAddress)
{
return !!m_libCec && m_libCec->SetPhysicalAddress(physicalAddress);
}
/// <summary>
/// Power on the given CEC capable devices. If CECDEVICE_BROADCAST is used, then wakeDevice in libcec_configuration will be used.
/// </summary>
/// <param name="logicalAddress">The logical address to power on.</param>
/// <returns>True when the command was sent successfully, false otherwise.</returns>
bool PowerOnDevices(CecLogicalAddress logicalAddress)
{
return !!m_libCec && m_libCec->PowerOnDevices((cec_logical_address) logicalAddress);
}
/// <summary>
/// Put the given CEC capable devices in standby mode. If CECDEVICE_BROADCAST is used, then standbyDevices in libcec_configuration will be used.
/// </summary>
/// <param name="logicalAddress">The logical address of the device to put in standby.</param>
/// <returns>True when the command was sent successfully, false otherwise.</returns>
bool StandbyDevices(CecLogicalAddress logicalAddress)
{
return !!m_libCec && m_libCec->StandbyDevices((cec_logical_address) logicalAddress);
}
/// <summary>
/// Sends a POLL message to a device, to check if it's present and responding.
/// </summary>
/// <param name="logicalAddress">The device to send the message to.</param>
/// <returns>True if the POLL was acked, false otherwise.</returns>
bool PollDevice(CecLogicalAddress logicalAddress)
{
return !!m_libCec && m_libCec->PollDevice((cec_logical_address) logicalAddress);
}
/// <summary>
/// Change the active source to a device type handled by libCEC. Use CEC_DEVICE_TYPE_RESERVED to make the default type used by libCEC active.
/// </summary>
/// <param name="type">The new active source. Use CEC_DEVICE_TYPE_RESERVED to use the primary type</param>
/// <returns>True when the command was sent successfully, false otherwise.</returns>
bool SetActiveSource(CecDeviceType type)
{
return !!m_libCec && m_libCec->SetActiveSource((cec_device_type) type);
}
/// <summary>
/// Change the deck control mode, if this adapter is registered as playback or recording device.
/// </summary>
/// <param name="mode">The new control mode.</param>
/// <param name="sendUpdate">True to send the new status over the CEC line.</param>
/// <returns>True if set, false otherwise.</returns>
bool SetDeckControlMode(CecDeckControlMode mode, bool sendUpdate)
{
return !!m_libCec && m_libCec->SetDeckControlMode((cec_deck_control_mode) mode, sendUpdate);
}
/// <summary>
/// Change the deck info, if this adapter is a playback or recording device.
/// </summary>
/// <param name="info">The new deck info.</param>
/// <param name="sendUpdate">True to send the new status over the CEC line.</param>
/// <returns>True if set, false otherwise.</returns>
bool SetDeckInfo(CecDeckInfo info, bool sendUpdate)
{
return !!m_libCec && m_libCec->SetDeckInfo((cec_deck_info) info, sendUpdate);
}
/// <summary>
/// Broadcast a message that notifies connected CEC capable devices that this device is no longer the active source.
/// </summary>
/// <returns>True when the command was sent successfully, false otherwise.</returns>
bool SetInactiveView(void)
{
return !!m_libCec && m_libCec->SetInactiveView();
}
/// <summary>
/// Change the menu state. This value is already changed by libCEC automatically if a device is (de)activated.
/// </summary>
/// <param name="state">The new state.</param>
/// <param name="sendUpdate">True to send the new status over the CEC line.</param>
/// <returns>True if set, false otherwise.</returns>
bool SetMenuState(CecMenuState state, bool sendUpdate)
{
return !!m_libCec && m_libCec->SetMenuState((cec_menu_state) state, sendUpdate);
}
/// <summary>
/// Display a message on the device with the given logical address. Not supported by most TVs.
/// </summary>
/// <param name="logicalAddress">The logical address of the device to display the message on.</param>
/// <param name="duration">The duration of the message</param>
/// <param name="message">The message to display.</param>
/// <returns>True when the command was sent, false otherwise.</returns>
bool SetOSDString(CecLogicalAddress logicalAddress, CecDisplayControl duration, String ^ message)
{
if (!m_libCec) {
return false;
}
marshal_context ^ context = gcnew marshal_context();
const char* strMessageC = context->marshal_as<const char*>(message);
bool bReturn = m_libCec->SetOSDString((cec_logical_address) logicalAddress, (cec_display_control) duration, strMessageC);
delete context;
return bReturn;
}
/// <summary>
/// Enable or disable monitoring mode, for debugging purposes. If monitoring mode is enabled, libCEC won't respond to any command, but only log incoming data.
/// </summary>
/// <param name="enable">True to enable, false to disable.</param>
/// <returns>True when switched successfully, false otherwise.</returns>
bool SwitchMonitoring(bool enable)
{
return !!m_libCec && m_libCec->SwitchMonitoring(enable);
}
/// <summary>
/// Get the CEC version of the device with the given logical address
/// </summary>
/// <param name="logicalAddress">The logical address of the device to get the CEC version for.</param>
/// <returns>The version or CEC_VERSION_UNKNOWN when the version couldn't be fetched.</returns>
CecVersion GetDeviceCecVersion(CecLogicalAddress logicalAddress)
{
if (!m_libCec) {
return CecVersion::Unknown;
}
return (CecVersion) m_libCec->GetDeviceCecVersion((cec_logical_address) logicalAddress);
}
/// <summary>
/// Get the menu language of the device with the given logical address
/// </summary>
/// <param name="logicalAddress">The logical address of the device to get the menu language for.</param>
/// <returns>The requested menu language.</returns>
String ^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress)
{
if (!m_libCec) {
return gcnew String("not connected");
}
std::string strLang = m_libCec->GetDeviceMenuLanguage((cec_logical_address)logicalAddress);
return gcnew String(strLang.c_str());
}
/// <summary>
/// Get the vendor ID of the device with the given logical address.
/// </summary>
/// <param name="logicalAddress">The logical address of the device to get the vendor ID for.</param>
/// <returns>The vendor ID or 0 if it wasn't found.</returns>
CecVendorId GetDeviceVendorId(CecLogicalAddress logicalAddress)
{
if (!m_libCec) {
return CecVendorId::Unknown;
}
return (CecVendorId)m_libCec->GetDeviceVendorId((cec_logical_address) logicalAddress);
}
/// <summary>
/// Get the power status of the device with the given logical address.
/// </summary>
/// <param name="logicalAddress">The logical address of the device to get the power status for.</param>
/// <returns>The power status or CEC_POWER_STATUS_UNKNOWN if it wasn't found.</returns>
CecPowerStatus GetDevicePowerStatus(CecLogicalAddress logicalAddress)
{
if (!m_libCec) {
return CecPowerStatus::Unknown;
}
return (CecPowerStatus) m_libCec->GetDevicePowerStatus((cec_logical_address) logicalAddress);
}
/// <summary>
/// Tell libCEC to poll for active devices on the bus.
/// </summary>
void RescanActiveDevices(void)
{
if (!!m_libCec) {
m_libCec->RescanActiveDevices();
}
}
/// <summary>
/// Get the logical addresses of the devices that are active on the bus, including those handled by libCEC.
/// </summary>
/// <returns>The logical addresses of the active devices</returns>
CecLogicalAddresses ^ GetActiveDevices(void)
{
CecLogicalAddresses ^ retVal = gcnew CecLogicalAddresses();
if (!m_libCec) {
return retVal;
}
unsigned int iDevices = 0;
cec_logical_addresses activeDevices = m_libCec->GetActiveDevices();
for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
if (activeDevices[iPtr])
retVal->Set((CecLogicalAddress)iPtr);
return retVal;
}
/// <summary>
/// Check whether a device is active on the bus.
/// </summary>
/// <param name="logicalAddress">The address to check.</param>
/// <returns>True when active, false otherwise.</returns>
bool IsActiveDevice(CecLogicalAddress logicalAddress)
{
return !!m_libCec && m_libCec->IsActiveDevice((cec_logical_address)logicalAddress);
}
/// <summary>
/// Check whether a device of the given type is active on the bus.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns>True when active, false otherwise.</returns>
bool IsActiveDeviceType(CecDeviceType type)
{
return !!m_libCec && m_libCec->IsActiveDeviceType((cec_device_type)type);
}
/// <summary>
/// Changes the active HDMI port.
/// </summary>
/// <param name="address">The device to which this libCEC is connected.</param>
/// <param name="port">The new port number.</param>
/// <returns>True when changed, false otherwise.</returns>
bool SetHDMIPort(CecLogicalAddress address, uint8_t port)
{
return !!m_libCec && m_libCec->SetHDMIPort((cec_logical_address)address, port);
}
/// <summary>
/// Sends a volume up keypress to an audiosystem if it's present.
/// </summary>
/// <param name="sendRelease">Send a key release after the keypress.</param>
/// <returns>The new audio status.</returns>
uint8_t VolumeUp(bool sendRelease)
{
if (!m_libCec) {
return 0;
}
return m_libCec->VolumeUp(sendRelease);
}
/// <summary>
/// Sends a volume down keypress to an audiosystem if it's present.
/// </summary>
/// <param name="sendRelease">Send a key release after the keypress.</param>
/// <returns>The new audio status.</returns>
uint8_t VolumeDown(bool sendRelease)
{
if (!m_libCec) {
return 0;
}
return m_libCec->VolumeDown(sendRelease);
}
/// <summary>
/// Sends a mute keypress to an audiosystem if it's present.
/// </summary>
/// <returns>The new audio status.</returns>
uint8_t MuteAudio()
{
if (!m_libCec) {
return 0;
}
return m_libCec->AudioToggleMute();
}
/// <summary>
/// Send a keypress to a device on the CEC bus.
/// </summary>
/// <param name="destination">The logical address of the device to send the message to.</param>
/// <param name="key">The key to send.</param>
/// <param name="wait">True to wait for a response, false otherwise.</param>
/// <returns>True when the keypress was acked, false otherwise.</returns>
bool SendKeypress(CecLogicalAddress destination, CecUserControlCode key, bool wait)
{
return !!m_libCec && m_libCec->SendKeypress((cec_logical_address)destination, (cec_user_control_code)key, wait);
}
/// <summary>
/// Send a key release to a device on the CEC bus.
/// </summary>
/// <param name="destination">The logical address of the device to send the message to.</param>
/// <param name="wait">True to wait for a response, false otherwise.</param>
/// <returns>True when the key release was acked, false otherwise.</returns>
bool SendKeyRelease(CecLogicalAddress destination, bool wait)
{
return !!m_libCec && m_libCec->SendKeyRelease((cec_logical_address)destination, wait);
}
/// <summary>
/// Get the OSD name of a device on the CEC bus.
/// </summary>
/// <param name="logicalAddress">The logical address of the device to get the OSD name for.</param>
/// <returns>The OSD name.</returns>
String ^ GetDeviceOSDName(CecLogicalAddress logicalAddress)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
std::string osdName = m_libCec->GetDeviceOSDName((cec_logical_address) logicalAddress);
// we need to terminate with \0, and we only got 14 chars in osd.name
char strOsdName[15];
strncpy_s(strOsdName, osdName.c_str(), 15);
return gcnew String(strOsdName);
}
/// <summary>
/// Get the logical address of the device that is currently the active source on the CEC bus.
/// </summary>
/// <returns>The active source or CECDEVICE_UNKNOWN when unknown.</returns>
CecLogicalAddress GetActiveSource()
{
if (!m_libCec) {
return CecLogicalAddress::Unknown;
}
return (CecLogicalAddress)m_libCec->GetActiveSource();
}
/// <summary>
/// Check whether a device is currently the active source on the CEC bus.
/// </summary>
/// <param name="logicalAddress">The logical address of the device to check.</param>
/// <returns>True when it is the active source, false otherwise.</returns>
bool IsActiveSource(CecLogicalAddress logicalAddress)
{
return !!m_libCec && m_libCec->IsActiveSource((cec_logical_address)logicalAddress);
}
/// <summary>
/// Get the physical address of the device with the given logical address.
/// </summary>
/// <param name="address">The logical address of the device to get the physical address for.</param>
/// <returns>The physical address or 0 if it wasn't found.</returns>
uint16_t GetDevicePhysicalAddress(CecLogicalAddress address)
{
if (!m_libCec) {
return 0xFFFF;
}
return m_libCec->GetDevicePhysicalAddress((cec_logical_address)address);
}
/// <summary>
/// Sets the stream path to the device on the given logical address.
/// </summary>
/// <param name="address">The address to activate.</param>
/// <returns>True when the command was sent, false otherwise.</returns>
bool SetStreamPath(CecLogicalAddress address)
{
return !!m_libCec && m_libCec->SetStreamPath((cec_logical_address)address);
}
/// <summary>
/// Sets the stream path to the device on the given physical address.
/// </summary>
/// <param name="physicalAddress">The address to activate.</param>
/// <returns>True when the command was sent, false otherwise.</returns>
bool SetStreamPath(uint16_t physicalAddress)
{
return !!m_libCec && m_libCec->SetStreamPath(physicalAddress);
}
/// <summary>
/// Get the list of logical addresses that libCEC is controlling
/// </summary>
/// <returns>The list of logical addresses that libCEC is controlling</returns>
CecLogicalAddresses ^GetLogicalAddresses(void)
{
CecLogicalAddresses ^addr = gcnew CecLogicalAddresses();
if (!m_libCec) {
return addr;
}
cec_logical_addresses libAddr = m_libCec->GetLogicalAddresses();
for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
addr->Addresses[iPtr] = (CecLogicalAddress)libAddr.addresses[iPtr];
addr->Primary = (CecLogicalAddress)libAddr.primary;
return addr;
}
/// <summary>
/// Get libCEC's current configuration.
/// </summary>
/// <param name="configuration">The configuration.</param>
/// <returns>True when the configuration was updated, false otherwise.</returns>
bool GetCurrentConfiguration(LibCECConfiguration ^configuration)
{
if (!m_libCec) {
return false;
}
libcec_configuration config;
config.Clear();
if (m_libCec->GetCurrentConfiguration(&config))
{
configuration->Update(config);
return true;
}
return false;
}
/// <summary>
/// Check whether the CEC adapter can save a configuration.
/// </summary>
/// <returns>True when this CEC adapter can save the user configuration, false otherwise.</returns>
#if CEC_LIB_VERSION_MAJOR >= 5
bool CanSaveConfiguration(void)
{
return !!m_libCec &&
m_libCec->CanSaveConfiguration();
}
#else
bool CanPersistConfiguration(void)
{
return !!m_libCec &&
m_libCec->CanPersistConfiguration();
}
bool PersistConfiguration(LibCECConfiguration ^configuration)
{
return SetConfiguration(configuration);
}
#endif
/// <summary>
/// Change libCEC's configuration. Store it updated settings in the eeprom of the device (if supported)
/// </summary>
/// <param name="configuration">The new configuration.</param>
/// <returns>True when the configuration was changed successfully, false otherwise.</returns>
bool SetConfiguration(LibCECConfiguration ^configuration)
{
if (!m_libCec) {
return false;
}
marshal_context ^ context = gcnew marshal_context();
libcec_configuration config;
ConvertConfiguration(context, configuration, config);
// don't update callbacks
config.callbacks = NULL;
bool bReturn = m_libCec->SetConfiguration(&config);
delete context;
return bReturn;
}
/// <summary>
/// Check whether libCEC is the active source on the bus.
/// </summary>
/// <returns>True when libCEC is the active source on the bus, false otherwise.</returns>
bool IsLibCECActiveSource()
{
return !!m_libCec && m_libCec->IsLibCECActiveSource();
}
/// <summary>
/// Get information about the given CEC adapter.
/// </summary>
/// <param name="port">The COM port to which the device is connected</param>
/// <param name="configuration">The device configuration</param>
/// <param name="timeoutMs">The timeout in milliseconds</param>
/// <returns>True when the device was found, false otherwise</returns>
bool GetDeviceInformation(String ^ port, LibCECConfiguration ^configuration, uint32_t timeoutMs)
{
if (!m_libCec) {
return false;
}
bool bReturn(false);
marshal_context ^ context = gcnew marshal_context();
libcec_configuration config;
config.Clear();
const char* strPortC = port->Length > 0 ? context->marshal_as<const char*>(port) : NULL;
if (m_libCec->GetDeviceInformation(strPortC, &config, timeoutMs))
{
configuration->Update(config);
bReturn = true;
}
delete context;
return bReturn;
}
String ^ ToString(CecLogicalAddress iAddress)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
const char *retVal = m_libCec->ToString((cec_logical_address)iAddress);
return gcnew String(retVal);
}
String ^ ToString(CecVendorId iVendorId)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
const char *retVal = m_libCec->ToString((cec_vendor_id)iVendorId);
return gcnew String(retVal);
}
String ^ ToString(CecVersion iVersion)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
const char *retVal = m_libCec->ToString((cec_version)iVersion);
return gcnew String(retVal);
}
String ^ ToString(CecPowerStatus iState)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
const char *retVal = m_libCec->ToString((cec_power_status)iState);
return gcnew String(retVal);
}
String ^ ToString(CecMenuState iState)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
const char *retVal = m_libCec->ToString((cec_menu_state)iState);
return gcnew String(retVal);
}
String ^ ToString(CecDeckControlMode iMode)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
const char *retVal = m_libCec->ToString((cec_deck_control_mode)iMode);
return gcnew String(retVal);
}
String ^ ToString(CecDeckInfo status)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
const char *retVal = m_libCec->ToString((cec_deck_info)status);
return gcnew String(retVal);
}
String ^ ToString(CecOpcode opcode)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
const char *retVal = m_libCec->ToString((cec_opcode)opcode);
return gcnew String(retVal);
}
String ^ ToString(CecSystemAudioStatus mode)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
const char *retVal = m_libCec->ToString((cec_system_audio_status)mode);
return gcnew String(retVal);
}
String ^ ToString(CecAudioStatus status)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
const char *retVal = m_libCec->ToString((cec_audio_status)status);
return gcnew String(retVal);
}
String ^ VersionToString(uint32_t version)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
char buf[20];
m_libCec->PrintVersion(version, buf, 20);
return gcnew String(buf);
}
String^ PhysicalAddressToString(uint16_t physicalAddress)
{
if (!m_libCec) {
return gcnew String("disconnected");
}
char buf[8];
_snprintf_s(buf, 8, "%X.%X.%X.%X", (physicalAddress >> 12) & 0xF, (physicalAddress >> 8) & 0xF, (physicalAddress >> 4) & 0xF, physicalAddress & 0xF);
return gcnew String(buf);
}
/// <summary>
/// Get a string with information about how libCEC was compiled.
/// </summary>
/// <returns>A string with information about how libCEC was compiled.</returns>
String ^ GetLibInfo()
{
if (!m_libCec) {
return gcnew String("disconnected");
}
const char *retVal = m_libCec->GetLibInfo();
return gcnew String(retVal);
}
/// <summary>
/// Calling this method will initialise the host on which libCEC is running.
/// On the RPi, it calls bcm_host_init(), which may only be called once per process, and is called by any process using
/// the video api on that system. So only call this method if libCEC is used in an application that
/// does not already initialise the video api.
/// </summary>
/// <remarks>Should be called as first call to libCEC, directly after CECInitialise() and before using Open()</remarks>
void InitVideoStandalone()
{
if (!!m_libCec) {
m_libCec->InitVideoStandalone();
}
}
#if CEC_LIB_VERSION_MAJOR >= 5
/// <summary>
/// Get statistics about traffic on the CEC line
/// </summary>
/// <returns>The requested statistics</returns>
CecAdapterStats ^GetStats(void)
{
struct cec_adapter_stats cstats;
return gcnew CecAdapterStats((!!m_libCec && m_libCec->GetStats(&cstats)) ? &cstats : NULL);
}
#endif
/// <summary>
/// Get the (virtual) USB vendor id
/// </summary>
/// <returns>The (virtual) USB vendor id</returns>
uint16_t GetAdapterVendorId()
{
if (!m_libCec) {
return 0;
}
return m_libCec->GetAdapterVendorId();
}
/// <summary>
/// Get the (virtual) USB product id
/// </summary>
/// <returns>The (virtual) USB product id</returns>
uint16_t GetAdapterProductId()
{
if (!m_libCec) {
return 0;
}
return m_libCec->GetAdapterProductId();
}
protected:
!LibCecSharp(void)
{
Destroy();
}
private:
void Destroy(void)
{
Close();
m_callbacks->Destroy();
m_libCec = NULL;
}
void ConvertConfiguration(marshal_context ^context, LibCECConfiguration ^netConfig, CEC::libcec_configuration &config)
{
config.Clear();
const char *strDeviceName = context->marshal_as<const char*>(netConfig->DeviceName);
memcpy_s(config.strDeviceName, 13, strDeviceName, 13);
for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
config.deviceTypes.types[iPtr] = (cec_device_type)netConfig->DeviceTypes->Types[iPtr];
config.bAutodetectAddress = netConfig->AutodetectAddress ? 1 : 0;
config.iPhysicalAddress = netConfig->PhysicalAddress;
config.baseDevice = (cec_logical_address)netConfig->BaseDevice;
config.iHDMIPort = netConfig->HDMIPort;
config.clientVersion = netConfig->ClientVersion;
config.bGetSettingsFromROM = netConfig->GetSettingsFromROM ? 1 : 0;
config.bActivateSource = netConfig->ActivateSource ? 1 : 0;
config.tvVendor = (cec_vendor_id)netConfig->TvVendor;
config.comboKey = (cec_user_control_code)netConfig->ComboKey;
config.iComboKeyTimeoutMs = netConfig->ComboKeyTimeoutMs;
config.iButtonRepeatRateMs = netConfig->ButtonRepeatRateMs;
config.iButtonReleaseDelayMs = netConfig->ButtonReleaseDelayMs;
config.iDoubleTapTimeoutMs = netConfig->DoubleTapTimeoutMs;
config.bAutoWakeAVR = netConfig->AutoWakeAVR ? 1 : 0;
#if CEC_LIB_VERSION_MAJOR >= 5
config.bAutoPowerOn = (uint8_t)netConfig->AutoPowerOn;
#endif
config.wakeDevices.Clear();
for (int iPtr = 0; iPtr < 16; iPtr++)
{
if (netConfig->WakeDevices->IsSet((CecLogicalAddress)iPtr))
config.wakeDevices.Set((cec_logical_address)iPtr);
}
config.powerOffDevices.Clear();
for (int iPtr = 0; iPtr < 16; iPtr++)
{
if (netConfig->PowerOffDevices->IsSet((CecLogicalAddress)iPtr))
config.powerOffDevices.Set((cec_logical_address)iPtr);
}
config.bPowerOffOnStandby = netConfig->PowerOffOnStandby ? 1 : 0;
const char *strDeviceLanguage = context->marshal_as<const char*>(netConfig->DeviceLanguage);
memcpy_s(config.strDeviceLanguage, 3, strDeviceLanguage, 3);
config.bMonitorOnly = netConfig->MonitorOnlyClient ? 1 : 0;
config.cecVersion = (cec_version)netConfig->CECVersion;
config.callbacks = GetLibCecCallbacks();
config.callbackParam = m_callbacks->Get();
}
ICECAdapter* m_libCec;
CecCallbackMethods ^ m_callbacks;
};
}