cec: updated changelog and bumped version numbers for 1.6.1
[deb_libcec.git] / src / LibCecSharp / CecSharpTypes.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2012 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 #include <windows.h>
35 #include <vcclr.h>
36 #include <msclr/marshal.h>
37 #include "../../include/cec.h"
38
39 #using <System.dll>
40
41 namespace CecSharp
42 {
43 public enum class CecDeviceType
44 {
45 Tv = 0,
46 RecordingDevice = 1,
47 Reserved = 2,
48 Tuner = 3,
49 PlaybackDevice = 4,
50 AudioSystem = 5
51 };
52
53 public enum class CecLogLevel
54 {
55 None = 0,
56 Error = 1,
57 Warning = 2,
58 Notice = 4,
59 Traffic = 8,
60 Debug = 16,
61 All = 31
62 };
63
64 public enum class CecLogicalAddress
65 {
66 Unknown = -1, //not a valid logical address
67 Tv = 0,
68 RecordingDevice1 = 1,
69 RecordingDevice2 = 2,
70 Tuner1 = 3,
71 PlaybackDevice1 = 4,
72 AudioSystem = 5,
73 Tuner2 = 6,
74 Tuner3 = 7,
75 PlaybackDevice2 = 8,
76 RecordingDevice3 = 9,
77 Tuner4 = 10,
78 PlaybackDevice3 = 11,
79 Reserved1 = 12,
80 Reserved2 = 13,
81 FreeUse = 14,
82 Unregistered = 15,
83 Broadcast = 15
84 };
85
86 public enum class CecAlert
87 {
88 ServiceDevice = 1
89 };
90
91 public enum class CecParameterType
92 {
93 ParameterTypeString = 1
94 };
95
96 public ref class CecParameter
97 {
98 public:
99 CecParameter(CecParameterType type, System::String ^ strData)
100 {
101 Type = type;
102 Data = strData;
103 }
104
105 property CecParameterType Type;
106 property System::String ^ Data;
107 };
108
109 public enum class CecPowerStatus
110 {
111 On = 0x00,
112 Standby = 0x01,
113 InTransitionStandbyToOn = 0x02,
114 InTransitionOnToStandby = 0x03,
115 Unknown = 0x99
116 };
117
118 public enum class CecVersion
119 {
120 Unknown = 0x00,
121 V1_2 = 0x01,
122 V1_2A = 0x02,
123 V1_3 = 0x03,
124 V1_3A = 0x04,
125 V1_4 = 0x05
126 };
127
128 public enum class CecDisplayControl
129 {
130 DisplayForDefaultTime = 0x00,
131 DisplayUntilCleared = 0x40,
132 ClearPreviousMessage = 0x80,
133 ReservedForFutureUse = 0xC0
134 };
135
136 public enum class CecMenuState
137 {
138 Activated = 0,
139 Deactivated = 1
140 };
141
142 public enum class CecDeckControlMode
143 {
144 SkipForwardWind = 1,
145 SkipReverseRewind = 2,
146 Stop = 3,
147 Eject = 4
148 };
149
150 public enum class CecDeckInfo
151 {
152 Play = 0x11,
153 Record = 0x12,
154 Reverse = 0x13,
155 Still = 0x14,
156 Slow = 0x15,
157 SlowReverse = 0x16,
158 FastForward = 0x17,
159 FastReverse = 0x18,
160 NoMedia = 0x19,
161 Stop = 0x1A,
162 SkipForwardWind = 0x1B,
163 SkipReverseRewind = 0x1C,
164 IndexSearchForward = 0x1D,
165 IndexSearchReverse = 0x1E,
166 OtherStatus = 0x1F
167 };
168
169 public enum class CecUserControlCode
170 {
171 Select = 0x00,
172 Up = 0x01,
173 Down = 0x02,
174 Left = 0x03,
175 Right = 0x04,
176 RightUp = 0x05,
177 RightDown = 0x06,
178 LeftUp = 0x07,
179 LeftDown = 0x08,
180 RootMenu = 0x09,
181 SetupMenu = 0x0A,
182 ContentsMenu = 0x0B,
183 FavoriteMenu = 0x0C,
184 Exit = 0x0D,
185 Number0 = 0x20,
186 Number1 = 0x21,
187 Number2 = 0x22,
188 Number3 = 0x23,
189 Number4 = 0x24,
190 Number5 = 0x25,
191 Number6 = 0x26,
192 Number7 = 0x27,
193 Number8 = 0x28,
194 Number9 = 0x29,
195 Dot = 0x2A,
196 Enter = 0x2B,
197 Clear = 0x2C,
198 NextFavorite = 0x2F,
199 ChannelUp = 0x30,
200 ChannelDown = 0x31,
201 PreviousChannel = 0x32,
202 SoundSelect = 0x33,
203 InputSelect = 0x34,
204 DisplayInformation = 0x35,
205 Help = 0x36,
206 PageUp = 0x37,
207 PageDown = 0x38,
208 Power = 0x40,
209 VolumeUp = 0x41,
210 VolumeDown = 0x42,
211 Mute = 0x43,
212 Play = 0x44,
213 Stop = 0x45,
214 Pause = 0x46,
215 Record = 0x47,
216 Rewind = 0x48,
217 FastForward = 0x49,
218 Eject = 0x4A,
219 Forward = 0x4B,
220 Backward = 0x4C,
221 StopRecord = 0x4D,
222 PauseRecord = 0x4E,
223 Angle = 0x50,
224 SubPicture = 0x51,
225 VideoOnDemand = 0x52,
226 ElectronicProgramGuide = 0x53,
227 TimerProgramming = 0x54,
228 InitialConfiguration = 0x55,
229 PlayFunction = 0x60,
230 PausePlayFunction = 0x61,
231 RecordFunction = 0x62,
232 PauseRecordFunction = 0x63,
233 StopFunction = 0x64,
234 MuteFunction = 0x65,
235 RestoreVolumeFunction = 0x66,
236 TuneFunction = 0x67,
237 SelectMediaFunction = 0x68,
238 SelectAVInputFunction = 0x69,
239 SelectAudioInputFunction = 0x6A,
240 PowerToggleFunction = 0x6B,
241 PowerOffFunction = 0x6C,
242 PowerOnFunction = 0x6D,
243 F1Blue = 0x71,
244 F2Red = 0X72,
245 F3Green = 0x73,
246 F4Yellow = 0x74,
247 F5 = 0x75,
248 Data = 0x76,
249 Max = 0x76,
250 Unknown
251 };
252
253 public enum class CecVendorId
254 {
255 Samsung = 0x00F0,
256 LG = 0xE091,
257 Panasonic = 0x8045,
258 Pioneer = 0xE036,
259 Onkyo = 0x09B0,
260 Yamaha = 0xA0DE,
261 Philips = 0x903E,
262 Sony = 0x080046,
263 Unknown = 0
264 };
265
266 public enum class CecAudioStatus
267 {
268 MuteStatusMask = 0x80,
269 VolumeStatusMask = 0x7F,
270 VolumeMin = 0x00,
271 VolumeMax = 0x64,
272 VolumeStatusUnknown = 0x7F
273 };
274
275 public enum class CecOpcode
276 {
277 ActiveSource = 0x82,
278 ImageViewOn = 0x04,
279 TextViewOn = 0x0D,
280 InactiveSource = 0x9D,
281 RequestActiveSource = 0x85,
282 RoutingChange = 0x80,
283 RoutingInformation = 0x81,
284 SetStreamPath = 0x86,
285 Standby = 0x36,
286 RecordOff = 0x0B,
287 RecordOn = 0x09,
288 RecordStatus = 0x0A,
289 RecordTvScreen = 0x0F,
290 ClearAnalogueTimer = 0x33,
291 ClearDigitalTimer = 0x99,
292 ClearExternalTimer = 0xA1,
293 SetAnalogueTimer = 0x34,
294 SetDigitalTimer = 0x97,
295 SetExternalTimer = 0xA2,
296 SetTimerProgramTitle = 0x67,
297 TimerClearedStatus = 0x43,
298 TimerStatus = 0x35,
299 CecVersion = 0x9E,
300 GetCecVersion = 0x9F,
301 GivePhysicalAddress = 0x83,
302 GetMenuLanguage = 0x91,
303 ReportPhysicalAddress = 0x84,
304 SetMenuLanguage = 0x32,
305 DeckControl = 0x42,
306 DeckStatus = 0x1B,
307 GiveDeckStatus = 0x1A,
308 Play = 0x41,
309 GiveTunerDeviceStatus = 0x08,
310 SelectAnalogueService = 0x92,
311 SelectDigtalService = 0x93,
312 TunerDeviceStatus = 0x07,
313 TunerStepDecrement = 0x06,
314 TunerStepIncrement = 0x05,
315 DeviceVendorId = 0x87,
316 GiveDeviceVendorId = 0x8C,
317 VendorCommand = 0x89,
318 VendorCommandWithId = 0xA0,
319 VendorRemoteButtonDown = 0x8A,
320 VendorRemoteButtonUp = 0x8B,
321 SetOsdString = 0x64,
322 GiveOsdName = 0x46,
323 SetOsdName = 0x47,
324 MenuRequest = 0x8D,
325 MenuStatus = 0x8E,
326 UserControlPressed = 0x44,
327 UserControlRelease = 0x45,
328 GiveDevicePowerStatus = 0x8F,
329 ReportPowerStatus = 0x90,
330 FeatureAbort = 0x00,
331 Abort = 0xFF,
332 GiveAudioStatus = 0x71,
333 GiveSystemAudioMode = 0x7D,
334 ReportAudioStatus = 0x7A,
335 SetSystemAudioMode = 0x72,
336 SystemAudioModeRequest = 0x70,
337 SystemAudioModeStatus = 0x7E,
338 SetAudioRate = 0x9A,
339 /* when this opcode is set, no opcode will be sent to the device. this is one of the reserved numbers */
340 None = 0xFD
341 };
342
343 public enum class CecSystemAudioStatus
344 {
345 Off = 0,
346 On = 1
347 };
348
349 public enum class CecClientVersion
350 {
351 VersionPre1_5 = 0,
352 Version1_5_0 = 0x1500,
353 Version1_5_1 = 0x1501,
354 Version1_5_2 = 0x1502,
355 Version1_5_3 = 0x1503,
356 Version1_6_0 = 0x1600,
357 Version1_6_1 = 0x1601
358 };
359
360 public enum class CecServerVersion
361 {
362 VersionPre1_5 = 0,
363 Version1_5_0 = 0x1500,
364 Version1_5_1 = 0x1501,
365 Version1_5_2 = 0x1502,
366 Version1_5_3 = 0x1503,
367 Version1_6_0 = 0x1600,
368 Version1_6_1 = 0x1601
369 };
370
371 public ref class CecAdapter
372 {
373 public:
374 CecAdapter(System::String ^ strPath, System::String ^ strComPort)
375 {
376 Path = strPath;
377 ComPort = strComPort;
378 }
379
380 property System::String ^ Path;
381 property System::String ^ ComPort;
382 };
383
384 public ref class CecDeviceTypeList
385 {
386 public:
387 CecDeviceTypeList(void)
388 {
389 Types = gcnew array<CecDeviceType>(5);
390 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
391 Types[iPtr] = CecDeviceType::Reserved;
392 }
393
394 property array<CecDeviceType> ^ Types;
395 };
396
397 public ref class CecLogicalAddresses
398 {
399 public:
400 CecLogicalAddresses(void)
401 {
402 Addresses = gcnew array<CecLogicalAddress>(16);
403 Clear();
404 }
405
406 void Clear(void)
407 {
408 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
409 Addresses[iPtr] = CecLogicalAddress::Unknown;
410 }
411
412 bool IsSet(CecLogicalAddress iAddress)
413 {
414 return Addresses[(unsigned int)iAddress] != CecLogicalAddress::Unknown;
415 }
416
417 void Set(CecLogicalAddress iAddress)
418 {
419 Addresses[(unsigned int)iAddress] = iAddress;
420 }
421
422 property CecLogicalAddress Primary;
423 property array<CecLogicalAddress> ^ Addresses;
424 };
425
426 public ref class CecDatapacket
427 {
428 public:
429 CecDatapacket(void)
430 {
431 Data = gcnew array<uint8_t>(100);
432 Size = 0;
433 }
434
435 void PushBack(uint8_t data)
436 {
437 if (Size < 100)
438 {
439 Data[Size] = data;
440 Size++;
441 }
442 }
443
444 property array<uint8_t> ^ Data;
445 property uint8_t Size;
446 };
447
448 public ref class CecCommand
449 {
450 public:
451 CecCommand(CecLogicalAddress iInitiator, CecLogicalAddress iDestination, bool bAck, bool bEom, CecOpcode iOpcode, int32_t iTransmitTimeout)
452 {
453 Initiator = iInitiator;
454 Destination = iDestination;
455 Ack = bAck;
456 Eom = bEom;
457 Opcode = iOpcode;
458 OpcodeSet = true;
459 TransmitTimeout = iTransmitTimeout;
460 Parameters = gcnew CecDatapacket;
461 Empty = false;
462 }
463
464 CecCommand(void)
465 {
466 Initiator = CecLogicalAddress::Unknown;
467 Destination = CecLogicalAddress::Unknown;
468 Ack = false;
469 Eom = false;
470 Opcode = CecOpcode::None;
471 OpcodeSet = false;
472 TransmitTimeout = 0;
473 Parameters = gcnew CecDatapacket;
474 Empty = true;
475 }
476
477 void PushBack(uint8_t data)
478 {
479 if (Initiator == CecLogicalAddress::Unknown && Destination == CecLogicalAddress::Unknown)
480 {
481 Initiator = (CecLogicalAddress) (data >> 4);
482 Destination = (CecLogicalAddress) (data & 0xF);
483 }
484 else if (!OpcodeSet)
485 {
486 OpcodeSet = true;
487 Opcode = (CecOpcode)data;
488 }
489 else
490 {
491 Parameters->PushBack(data);
492 }
493 }
494
495 property bool Empty;
496 property CecLogicalAddress Initiator;
497 property CecLogicalAddress Destination;
498 property bool Ack;
499 property bool Eom;
500 property CecOpcode Opcode;
501 property CecDatapacket ^ Parameters;
502 property bool OpcodeSet;
503 property int32_t TransmitTimeout;
504 };
505
506 public ref class CecKeypress
507 {
508 public:
509 CecKeypress(int iKeycode, unsigned int iDuration)
510 {
511 Keycode = iKeycode;
512 Duration = iDuration;
513 Empty = false;
514 }
515
516 CecKeypress(void)
517 {
518 Keycode = 0;
519 Duration = 0;
520 Empty = true;
521 }
522
523 property bool Empty;
524 property int Keycode;
525 property unsigned int Duration;
526 };
527
528 public ref class CecLogMessage
529 {
530 public:
531 CecLogMessage(System::String ^ strMessage, CecLogLevel iLevel, int64_t iTime)
532 {
533 Message = strMessage;
534 Level = iLevel;
535 Time = iTime;
536 Empty = false;
537 }
538
539 CecLogMessage(void)
540 {
541 Message = "";
542 Level = CecLogLevel::None;
543 Time = 0;
544 Empty = true;
545 }
546
547 property bool Empty;
548 property System::String ^Message;
549 property CecLogLevel Level;
550 property int64_t Time;
551 };
552
553 ref class CecCallbackMethods; //forward
554 public ref class LibCECConfiguration
555 {
556 public:
557 LibCECConfiguration(void)
558 {
559 DeviceName = "";
560 DeviceTypes = gcnew CecDeviceTypeList();
561 AutodetectAddress = true;
562 PhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS;
563 BaseDevice = (CecLogicalAddress)CEC_DEFAULT_BASE_DEVICE;
564 HDMIPort = CEC_DEFAULT_HDMI_PORT;
565 ClientVersion = CecClientVersion::VersionPre1_5;
566 ServerVersion = CecServerVersion::VersionPre1_5;
567 TvVendor = CecVendorId::Unknown;
568
569 GetSettingsFromROM = false;
570 UseTVMenuLanguage = CEC_DEFAULT_SETTING_USE_TV_MENU_LANGUAGE == 1;
571 ActivateSource = CEC_DEFAULT_SETTING_ACTIVATE_SOURCE == 1;
572
573 WakeDevices = gcnew CecLogicalAddresses();
574 if (CEC_DEFAULT_SETTING_ACTIVATE_SOURCE == 1)
575 WakeDevices->Set(CecLogicalAddress::Tv);
576
577 PowerOffDevices = gcnew CecLogicalAddresses();
578 if (CEC_DEFAULT_SETTING_POWER_OFF_SHUTDOWN == 1)
579 PowerOffDevices->Set(CecLogicalAddress::Broadcast);
580
581 PowerOffScreensaver = CEC_DEFAULT_SETTING_POWER_OFF_SCREENSAVER == 1;
582 PowerOffOnStandby = CEC_DEFAULT_SETTING_POWER_OFF_ON_STANDBY == 1;
583 SendInactiveSource = CEC_DEFAULT_SETTING_SEND_INACTIVE_SOURCE == 1;
584 LogicalAddresses = gcnew CecLogicalAddresses();
585 FirmwareVersion = 1;
586 PowerOffDevicesOnStandby = CEC_DEFAULT_SETTING_POWER_OFF_DEVICES_STANDBY == 1;
587 }
588
589 void SetCallbacks(CecCallbackMethods ^callbacks)
590 {
591 Callbacks = callbacks;
592 }
593
594 void Update(const CEC::libcec_configuration &config)
595 {
596 DeviceName = gcnew System::String(config.strDeviceName);
597
598 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
599 DeviceTypes->Types[iPtr] = (CecDeviceType)config.deviceTypes.types[iPtr];
600
601 AutodetectAddress = config.bAutodetectAddress == 1;
602 PhysicalAddress = config.iPhysicalAddress;
603 BaseDevice = (CecLogicalAddress)config.baseDevice;
604 HDMIPort = config.iHDMIPort;
605 ClientVersion = (CecClientVersion)config.clientVersion;
606 ServerVersion = (CecServerVersion)config.serverVersion;
607 TvVendor = (CecVendorId)config.tvVendor;
608
609 // player specific settings
610 GetSettingsFromROM = config.bGetSettingsFromROM == 1;
611 UseTVMenuLanguage = config.bUseTVMenuLanguage == 1;
612 ActivateSource = config.bActivateSource == 1;
613
614 WakeDevices->Clear();
615 for (uint8_t iPtr = 0; iPtr <= 16; iPtr++)
616 if (config.wakeDevices[iPtr])
617 WakeDevices->Set((CecLogicalAddress)iPtr);
618
619 PowerOffDevices->Clear();
620 for (uint8_t iPtr = 0; iPtr <= 16; iPtr++)
621 if (config.powerOffDevices[iPtr])
622 PowerOffDevices->Set((CecLogicalAddress)iPtr);
623
624 PowerOffScreensaver = config.bPowerOffScreensaver == 1;
625 PowerOffOnStandby = config.bPowerOffOnStandby == 1;
626
627 if (ServerVersion >= CecServerVersion::Version1_5_1)
628 SendInactiveSource = config.bSendInactiveSource == 1;
629
630 if (ServerVersion >= CecServerVersion::Version1_5_3)
631 {
632 LogicalAddresses->Clear();
633 for (uint8_t iPtr = 0; iPtr <= 16; iPtr++)
634 if (config.logicalAddresses[iPtr])
635 LogicalAddresses->Set((CecLogicalAddress)iPtr);
636 }
637
638 if (ServerVersion >= CecServerVersion::Version1_6_0)
639 {
640 FirmwareVersion = config.iFirmwareVersion;
641 PowerOffDevicesOnStandby = config.bPowerOffDevicesOnStandby == 1;
642 }
643 }
644
645 property System::String ^ DeviceName;
646 property CecDeviceTypeList ^ DeviceTypes;
647 property bool AutodetectAddress;
648 property uint16_t PhysicalAddress;
649 property CecLogicalAddress BaseDevice;
650 property uint8_t HDMIPort;
651 property CecClientVersion ClientVersion;
652 property CecServerVersion ServerVersion;
653 property CecVendorId TvVendor;
654
655 // player specific settings
656 property bool GetSettingsFromROM;
657 property bool UseTVMenuLanguage;
658 property bool ActivateSource;
659 property CecLogicalAddresses ^WakeDevices;
660 property CecLogicalAddresses ^PowerOffDevices;
661 property bool PowerOffScreensaver;
662 property bool PowerOffOnStandby;
663 property bool SendInactiveSource;
664 property CecLogicalAddresses ^LogicalAddresses;
665 property uint16_t FirmwareVersion;
666 property bool PowerOffDevicesOnStandby;
667
668 property CecCallbackMethods ^ Callbacks;
669 };
670
671 // the callback methods are called by unmanaged code, so we need some delegates for this
672 #pragma unmanaged
673 // unmanaged callback methods
674 typedef int (__stdcall *LOGCB) (const CEC::cec_log_message &message);
675 typedef int (__stdcall *KEYCB) (const CEC::cec_keypress &key);
676 typedef int (__stdcall *COMMANDCB)(const CEC::cec_command &command);
677 typedef int (__stdcall *CONFIGCB) (const CEC::libcec_configuration &config);
678 typedef int (__stdcall *ALERTCB) (const CEC::libcec_alert, const CEC::libcec_parameter &data);
679
680 static LOGCB g_logCB;
681 static KEYCB g_keyCB;
682 static COMMANDCB g_commandCB;
683 static CONFIGCB g_configCB;
684 static ALERTCB g_alertCB;
685 static CEC::ICECCallbacks g_cecCallbacks;
686
687 int CecLogMessageCB(void *cbParam, const CEC::cec_log_message &message)
688 {
689 if (g_logCB)
690 return g_logCB(message);
691 return 0;
692 }
693
694 int CecKeyPressCB(void *cbParam, const CEC::cec_keypress &key)
695 {
696 if (g_keyCB)
697 return g_keyCB(key);
698 return 0;
699 }
700
701 int CecCommandCB(void *cbParam, const CEC::cec_command &command)
702 {
703 if (g_commandCB)
704 return g_commandCB(command);
705 return 0;
706 }
707
708 int CecConfigCB(void *cbParam, const CEC::libcec_configuration &config)
709 {
710 if (g_configCB)
711 return g_configCB(config);
712 return 0;
713 }
714
715 int CecAlertCB(void *cbParam, const CEC::libcec_alert alert, const CEC::libcec_parameter &data)
716 {
717 if (g_alertCB)
718 return g_alertCB(alert, data);
719 return 0;
720 }
721
722 #pragma managed
723 // delegates for the unmanaged callback methods
724 public delegate int CecLogMessageManagedDelegate(const CEC::cec_log_message &);
725 public delegate int CecKeyPressManagedDelegate(const CEC::cec_keypress &);
726 public delegate int CecCommandManagedDelegate(const CEC::cec_command &);
727 public delegate int CecConfigManagedDelegate(const CEC::libcec_configuration &);
728 public delegate int CecAlertManagedDelegate(const CEC::libcec_alert, const CEC::libcec_parameter &);
729
730 // callback method interface
731 public ref class CecCallbackMethods
732 {
733 public:
734 CecCallbackMethods(void)
735 {
736 m_bHasCallbacks = false;
737 m_bDelegatesCreated = false;
738 }
739
740 ~CecCallbackMethods(void)
741 {
742 DestroyDelegates();
743 }
744
745 protected:
746 !CecCallbackMethods(void)
747 {
748 DestroyDelegates();
749 }
750
751 public:
752 virtual void DisableCallbacks(void)
753 {
754 DestroyDelegates();
755 }
756
757 virtual bool EnableCallbacks(CecCallbackMethods ^ callbacks)
758 {
759 CreateDelegates();
760 if (!m_bHasCallbacks)
761 {
762 m_bHasCallbacks = true;
763 m_callbacks = callbacks;
764 return true;
765 }
766
767 return false;
768 }
769
770 virtual int ReceiveLogMessage(CecLogMessage ^ message)
771 {
772 return 0;
773 }
774
775 virtual int ReceiveKeypress(CecKeypress ^ key)
776 {
777 return 0;
778 }
779
780 virtual int ReceiveCommand(CecCommand ^ command)
781 {
782 return 0;
783 }
784
785 virtual int ConfigurationChanged(LibCECConfiguration ^ config)
786 {
787 return 0;
788 }
789
790 virtual int ReceiveAlert(CecAlert alert, CecParameter ^ data)
791 {
792 return 0;
793 }
794
795 protected:
796 // managed callback methods
797 int CecLogMessageManaged(const CEC::cec_log_message &message)
798 {
799 int iReturn(0);
800 if (m_bHasCallbacks)
801 iReturn = m_callbacks->ReceiveLogMessage(gcnew CecLogMessage(gcnew System::String(message.message), (CecLogLevel)message.level, message.time));
802 return iReturn;
803 }
804
805 int CecKeyPressManaged(const CEC::cec_keypress &key)
806 {
807 int iReturn(0);
808 if (m_bHasCallbacks)
809 iReturn = m_callbacks->ReceiveKeypress(gcnew CecKeypress(key.keycode, key.duration));
810 return iReturn;
811 }
812
813 int CecCommandManaged(const CEC::cec_command &command)
814 {
815 int iReturn(0);
816 if (m_bHasCallbacks)
817 {
818 CecCommand ^ newCommand = gcnew CecCommand((CecLogicalAddress)command.initiator, (CecLogicalAddress)command.destination, command.ack == 1 ? true : false, command.eom == 1 ? true : false, (CecOpcode)command.opcode, command.transmit_timeout);
819 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
820 newCommand->Parameters->PushBack(command.parameters[iPtr]);
821 iReturn = m_callbacks->ReceiveCommand(newCommand);
822 }
823 return iReturn;
824 }
825
826 int CecConfigManaged(const CEC::libcec_configuration &config)
827 {
828 int iReturn(0);
829 if (m_bHasCallbacks)
830 {
831 LibCECConfiguration ^netConfig = gcnew LibCECConfiguration();
832 netConfig->Update(config);
833 iReturn = m_callbacks->ConfigurationChanged(netConfig);
834 }
835 return iReturn;
836 }
837
838 int CecAlertManaged(const CEC::libcec_alert alert, const CEC::libcec_parameter &data)
839 {
840 int iReturn(0);
841 if (m_bHasCallbacks)
842 {
843 CecParameterType newType = (CecParameterType)data.paramType;
844 if (newType == CecParameterType::ParameterTypeString)
845 {
846 System::String ^ newData = gcnew System::String((const char *)data.paramData, 0, 128);
847 CecParameter ^ newParam = gcnew CecParameter(newType, newData);
848 iReturn = m_callbacks->ReceiveAlert((CecAlert)alert, newParam);
849 }
850 }
851 return iReturn;
852 }
853
854 void DestroyDelegates()
855 {
856 m_bHasCallbacks = false;
857 if (m_bDelegatesCreated)
858 {
859 m_bDelegatesCreated = false;
860 m_logMessageGCHandle.Free();
861 m_keypressGCHandle.Free();
862 m_commandGCHandle.Free();
863 }
864 }
865
866 void CreateDelegates()
867 {
868 DestroyDelegates();
869
870 if (!m_bDelegatesCreated)
871 {
872 msclr::interop::marshal_context ^ context = gcnew msclr::interop::marshal_context();
873
874 // create the delegate method for the log message callback
875 m_logMessageDelegate = gcnew CecLogMessageManagedDelegate(this, &CecCallbackMethods::CecLogMessageManaged);
876 m_logMessageGCHandle = System::Runtime::InteropServices::GCHandle::Alloc(m_logMessageDelegate);
877 g_logCB = static_cast<LOGCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_logMessageDelegate).ToPointer());
878 g_cecCallbacks.CBCecLogMessage = CecLogMessageCB;
879
880 // create the delegate method for the keypress callback
881 m_keypressDelegate = gcnew CecKeyPressManagedDelegate(this, &CecCallbackMethods::CecKeyPressManaged);
882 m_keypressGCHandle = System::Runtime::InteropServices::GCHandle::Alloc(m_keypressDelegate);
883 g_keyCB = static_cast<KEYCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_keypressDelegate).ToPointer());
884 g_cecCallbacks.CBCecKeyPress = CecKeyPressCB;
885
886 // create the delegate method for the command callback
887 m_commandDelegate = gcnew CecCommandManagedDelegate(this, &CecCallbackMethods::CecCommandManaged);
888 m_commandGCHandle = System::Runtime::InteropServices::GCHandle::Alloc(m_commandDelegate);
889 g_commandCB = static_cast<COMMANDCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_commandDelegate).ToPointer());
890 g_cecCallbacks.CBCecCommand = CecCommandCB;
891
892 // create the delegate method for the configuration change callback
893 m_configDelegate = gcnew CecConfigManagedDelegate(this, &CecCallbackMethods::CecConfigManaged);
894 m_configGCHandle = System::Runtime::InteropServices::GCHandle::Alloc(m_configDelegate);
895 g_configCB = static_cast<CONFIGCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_configDelegate).ToPointer());
896 g_cecCallbacks.CBCecConfigurationChanged = CecConfigCB;
897
898 // create the delegate method for the alert callback
899 m_alertDelegate = gcnew CecAlertManagedDelegate(this, &CecCallbackMethods::CecAlertManaged);
900 m_alertGCHandle = System::Runtime::InteropServices::GCHandle::Alloc(m_alertDelegate);
901 g_alertCB = static_cast<ALERTCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_alertDelegate).ToPointer());
902 g_cecCallbacks.CBCecAlert = CecAlertCB;
903
904 delete context;
905 m_bDelegatesCreated = true;
906 }
907 }
908
909 CecLogMessageManagedDelegate ^ m_logMessageDelegate;
910 static System::Runtime::InteropServices::GCHandle m_logMessageGCHandle;
911 LOGCB m_logMessageCallback;
912
913 CecKeyPressManagedDelegate ^ m_keypressDelegate;
914 static System::Runtime::InteropServices::GCHandle m_keypressGCHandle;
915 KEYCB m_keypressCallback;
916
917 CecCommandManagedDelegate ^ m_commandDelegate;
918 static System::Runtime::InteropServices::GCHandle m_commandGCHandle;
919 COMMANDCB m_commandCallback;
920
921 CecConfigManagedDelegate ^ m_configDelegate;
922 static System::Runtime::InteropServices::GCHandle m_configGCHandle;
923 CONFIGCB m_configCallback;
924
925 CecAlertManagedDelegate ^ m_alertDelegate;
926 static System::Runtime::InteropServices::GCHandle m_alertGCHandle;
927 CONFIGCB m_alertCallback;
928
929 CecCallbackMethods ^ m_callbacks;
930 bool m_bHasCallbacks;
931 bool m_bDelegatesCreated;
932 };
933 }