cec: added v1.6.2 enum values to LibCecSharp
[deb_libcec.git] / src / LibCecSharp / CecSharpTypes.h
CommitLineData
4ef3b314
LOK
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
41namespace 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
6e919697
MK
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
4ef3b314
LOK
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,
de90f347 262 Sony = 0x080046,
4ef3b314
LOK
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,
a8fffd1b 352 Version1_5_0 = 0x1500,
e4a7396c
LOK
353 Version1_5_1 = 0x1501,
354 Version1_5_2 = 0x1502,
355 Version1_5_3 = 0x1503,
356 Version1_6_0 = 0x1600,
6d0364f9
LOK
357 Version1_6_1 = 0x1601,
358 Version1_6_2 = 0x1602
4ef3b314
LOK
359 };
360
e4a7396c 361 public enum class CecServerVersion
3efda01a
LOK
362 {
363 VersionPre1_5 = 0,
a8fffd1b 364 Version1_5_0 = 0x1500,
e4a7396c
LOK
365 Version1_5_1 = 0x1501,
366 Version1_5_2 = 0x1502,
367 Version1_5_3 = 0x1503,
368 Version1_6_0 = 0x1600,
6d0364f9
LOK
369 Version1_6_1 = 0x1601,
370 Version1_6_2 = 0x1602
3efda01a
LOK
371 };
372
4ef3b314
LOK
373 public ref class CecAdapter
374 {
375 public:
376 CecAdapter(System::String ^ strPath, System::String ^ strComPort)
377 {
378 Path = strPath;
379 ComPort = strComPort;
380 }
381
382 property System::String ^ Path;
383 property System::String ^ ComPort;
384 };
385
386 public ref class CecDeviceTypeList
387 {
388 public:
389 CecDeviceTypeList(void)
390 {
391 Types = gcnew array<CecDeviceType>(5);
392 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
393 Types[iPtr] = CecDeviceType::Reserved;
394 }
395
396 property array<CecDeviceType> ^ Types;
397 };
398
399 public ref class CecLogicalAddresses
400 {
401 public:
402 CecLogicalAddresses(void)
403 {
404 Addresses = gcnew array<CecLogicalAddress>(16);
b7907707
LOK
405 Clear();
406 }
407
408 void Clear(void)
409 {
4ef3b314 410 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
63851508 411 Addresses[iPtr] = CecLogicalAddress::Unknown;
4ef3b314
LOK
412 }
413
414 bool IsSet(CecLogicalAddress iAddress)
415 {
63851508 416 return Addresses[(unsigned int)iAddress] != CecLogicalAddress::Unknown;
4ef3b314
LOK
417 }
418
b7907707
LOK
419 void Set(CecLogicalAddress iAddress)
420 {
421 Addresses[(unsigned int)iAddress] = iAddress;
422 }
423
a9fb46b4 424 property CecLogicalAddress Primary;
4ef3b314
LOK
425 property array<CecLogicalAddress> ^ Addresses;
426 };
427
428 public ref class CecDatapacket
429 {
430 public:
431 CecDatapacket(void)
432 {
433 Data = gcnew array<uint8_t>(100);
434 Size = 0;
435 }
436
437 void PushBack(uint8_t data)
438 {
439 if (Size < 100)
440 {
441 Data[Size] = data;
442 Size++;
443 }
444 }
445
446 property array<uint8_t> ^ Data;
447 property uint8_t Size;
448 };
449
450 public ref class CecCommand
451 {
452 public:
453 CecCommand(CecLogicalAddress iInitiator, CecLogicalAddress iDestination, bool bAck, bool bEom, CecOpcode iOpcode, int32_t iTransmitTimeout)
454 {
455 Initiator = iInitiator;
456 Destination = iDestination;
457 Ack = bAck;
458 Eom = bEom;
459 Opcode = iOpcode;
460 OpcodeSet = true;
461 TransmitTimeout = iTransmitTimeout;
462 Parameters = gcnew CecDatapacket;
463 Empty = false;
464 }
465
466 CecCommand(void)
467 {
468 Initiator = CecLogicalAddress::Unknown;
469 Destination = CecLogicalAddress::Unknown;
470 Ack = false;
471 Eom = false;
472 Opcode = CecOpcode::None;
473 OpcodeSet = false;
474 TransmitTimeout = 0;
475 Parameters = gcnew CecDatapacket;
476 Empty = true;
477 }
478
479 void PushBack(uint8_t data)
480 {
481 if (Initiator == CecLogicalAddress::Unknown && Destination == CecLogicalAddress::Unknown)
482 {
483 Initiator = (CecLogicalAddress) (data >> 4);
484 Destination = (CecLogicalAddress) (data & 0xF);
485 }
486 else if (!OpcodeSet)
487 {
488 OpcodeSet = true;
489 Opcode = (CecOpcode)data;
490 }
491 else
492 {
493 Parameters->PushBack(data);
494 }
495 }
496
497 property bool Empty;
498 property CecLogicalAddress Initiator;
499 property CecLogicalAddress Destination;
500 property bool Ack;
501 property bool Eom;
502 property CecOpcode Opcode;
503 property CecDatapacket ^ Parameters;
504 property bool OpcodeSet;
505 property int32_t TransmitTimeout;
506 };
507
508 public ref class CecKeypress
509 {
510 public:
511 CecKeypress(int iKeycode, unsigned int iDuration)
512 {
513 Keycode = iKeycode;
514 Duration = iDuration;
515 Empty = false;
516 }
517
518 CecKeypress(void)
519 {
520 Keycode = 0;
521 Duration = 0;
522 Empty = true;
523 }
524
525 property bool Empty;
526 property int Keycode;
527 property unsigned int Duration;
528 };
529
530 public ref class CecLogMessage
531 {
532 public:
533 CecLogMessage(System::String ^ strMessage, CecLogLevel iLevel, int64_t iTime)
534 {
535 Message = strMessage;
536 Level = iLevel;
537 Time = iTime;
538 Empty = false;
539 }
540
541 CecLogMessage(void)
542 {
543 Message = "";
544 Level = CecLogLevel::None;
545 Time = 0;
546 Empty = true;
547 }
548
549 property bool Empty;
550 property System::String ^Message;
551 property CecLogLevel Level;
552 property int64_t Time;
553 };
554
32403cc3
LOK
555 ref class CecCallbackMethods; //forward
556 public ref class LibCECConfiguration
557 {
558 public:
559 LibCECConfiguration(void)
560 {
561 DeviceName = "";
562 DeviceTypes = gcnew CecDeviceTypeList();
8670c970 563 AutodetectAddress = true;
32403cc3
LOK
564 PhysicalAddress = CEC_DEFAULT_PHYSICAL_ADDRESS;
565 BaseDevice = (CecLogicalAddress)CEC_DEFAULT_BASE_DEVICE;
566 HDMIPort = CEC_DEFAULT_HDMI_PORT;
567 ClientVersion = CecClientVersion::VersionPre1_5;
3efda01a 568 ServerVersion = CecServerVersion::VersionPre1_5;
b7907707 569 TvVendor = CecVendorId::Unknown;
32403cc3
LOK
570
571 GetSettingsFromROM = false;
572 UseTVMenuLanguage = CEC_DEFAULT_SETTING_USE_TV_MENU_LANGUAGE == 1;
b7907707
LOK
573 ActivateSource = CEC_DEFAULT_SETTING_ACTIVATE_SOURCE == 1;
574
575 WakeDevices = gcnew CecLogicalAddresses();
576 if (CEC_DEFAULT_SETTING_ACTIVATE_SOURCE == 1)
577 WakeDevices->Set(CecLogicalAddress::Tv);
578
579 PowerOffDevices = gcnew CecLogicalAddresses();
580 if (CEC_DEFAULT_SETTING_POWER_OFF_SHUTDOWN == 1)
581 PowerOffDevices->Set(CecLogicalAddress::Broadcast);
582
32403cc3
LOK
583 PowerOffScreensaver = CEC_DEFAULT_SETTING_POWER_OFF_SCREENSAVER == 1;
584 PowerOffOnStandby = CEC_DEFAULT_SETTING_POWER_OFF_ON_STANDBY == 1;
a8fffd1b 585 SendInactiveSource = CEC_DEFAULT_SETTING_SEND_INACTIVE_SOURCE == 1;
b98fc43d
LOK
586 LogicalAddresses = gcnew CecLogicalAddresses();
587 FirmwareVersion = 1;
588 PowerOffDevicesOnStandby = CEC_DEFAULT_SETTING_POWER_OFF_DEVICES_STANDBY == 1;
32403cc3
LOK
589 }
590
591 void SetCallbacks(CecCallbackMethods ^callbacks)
592 {
593 Callbacks = callbacks;
594 }
595
3efda01a
LOK
596 void Update(const CEC::libcec_configuration &config)
597 {
598 DeviceName = gcnew System::String(config.strDeviceName);
599
600 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
601 DeviceTypes->Types[iPtr] = (CecDeviceType)config.deviceTypes.types[iPtr];
602
603 AutodetectAddress = config.bAutodetectAddress == 1;
604 PhysicalAddress = config.iPhysicalAddress;
605 BaseDevice = (CecLogicalAddress)config.baseDevice;
606 HDMIPort = config.iHDMIPort;
607 ClientVersion = (CecClientVersion)config.clientVersion;
608 ServerVersion = (CecServerVersion)config.serverVersion;
609 TvVendor = (CecVendorId)config.tvVendor;
610
611 // player specific settings
612 GetSettingsFromROM = config.bGetSettingsFromROM == 1;
613 UseTVMenuLanguage = config.bUseTVMenuLanguage == 1;
614 ActivateSource = config.bActivateSource == 1;
615
616 WakeDevices->Clear();
617 for (uint8_t iPtr = 0; iPtr <= 16; iPtr++)
618 if (config.wakeDevices[iPtr])
619 WakeDevices->Set((CecLogicalAddress)iPtr);
620
621 PowerOffDevices->Clear();
622 for (uint8_t iPtr = 0; iPtr <= 16; iPtr++)
623 if (config.powerOffDevices[iPtr])
624 PowerOffDevices->Set((CecLogicalAddress)iPtr);
625
626 PowerOffScreensaver = config.bPowerOffScreensaver == 1;
627 PowerOffOnStandby = config.bPowerOffOnStandby == 1;
b98fc43d
LOK
628
629 if (ServerVersion >= CecServerVersion::Version1_5_1)
630 SendInactiveSource = config.bSendInactiveSource == 1;
631
632 if (ServerVersion >= CecServerVersion::Version1_5_3)
633 {
634 LogicalAddresses->Clear();
635 for (uint8_t iPtr = 0; iPtr <= 16; iPtr++)
636 if (config.logicalAddresses[iPtr])
637 LogicalAddresses->Set((CecLogicalAddress)iPtr);
638 }
639
640 if (ServerVersion >= CecServerVersion::Version1_6_0)
641 {
642 FirmwareVersion = config.iFirmwareVersion;
643 PowerOffDevicesOnStandby = config.bPowerOffDevicesOnStandby == 1;
644 }
3efda01a
LOK
645 }
646
b7907707
LOK
647 property System::String ^ DeviceName;
648 property CecDeviceTypeList ^ DeviceTypes;
8670c970 649 property bool AutodetectAddress;
b7907707
LOK
650 property uint16_t PhysicalAddress;
651 property CecLogicalAddress BaseDevice;
652 property uint8_t HDMIPort;
653 property CecClientVersion ClientVersion;
3efda01a 654 property CecServerVersion ServerVersion;
b7907707 655 property CecVendorId TvVendor;
32403cc3
LOK
656
657 // player specific settings
b7907707
LOK
658 property bool GetSettingsFromROM;
659 property bool UseTVMenuLanguage;
660 property bool ActivateSource;
661 property CecLogicalAddresses ^WakeDevices;
662 property CecLogicalAddresses ^PowerOffDevices;
663 property bool PowerOffScreensaver;
664 property bool PowerOffOnStandby;
a8fffd1b 665 property bool SendInactiveSource;
b98fc43d
LOK
666 property CecLogicalAddresses ^LogicalAddresses;
667 property uint16_t FirmwareVersion;
668 property bool PowerOffDevicesOnStandby;
b7907707
LOK
669
670 property CecCallbackMethods ^ Callbacks;
32403cc3
LOK
671 };
672
4ef3b314
LOK
673 // the callback methods are called by unmanaged code, so we need some delegates for this
674 #pragma unmanaged
675 // unmanaged callback methods
676 typedef int (__stdcall *LOGCB) (const CEC::cec_log_message &message);
677 typedef int (__stdcall *KEYCB) (const CEC::cec_keypress &key);
678 typedef int (__stdcall *COMMANDCB)(const CEC::cec_command &command);
32403cc3 679 typedef int (__stdcall *CONFIGCB) (const CEC::libcec_configuration &config);
6e919697 680 typedef int (__stdcall *ALERTCB) (const CEC::libcec_alert, const CEC::libcec_parameter &data);
4ef3b314
LOK
681
682 static LOGCB g_logCB;
683 static KEYCB g_keyCB;
684 static COMMANDCB g_commandCB;
32403cc3 685 static CONFIGCB g_configCB;
6e919697 686 static ALERTCB g_alertCB;
4ef3b314
LOK
687 static CEC::ICECCallbacks g_cecCallbacks;
688
689 int CecLogMessageCB(void *cbParam, const CEC::cec_log_message &message)
690 {
691 if (g_logCB)
692 return g_logCB(message);
693 return 0;
694 }
695
696 int CecKeyPressCB(void *cbParam, const CEC::cec_keypress &key)
697 {
698 if (g_keyCB)
699 return g_keyCB(key);
700 return 0;
701 }
702
703 int CecCommandCB(void *cbParam, const CEC::cec_command &command)
704 {
705 if (g_commandCB)
706 return g_commandCB(command);
707 return 0;
708 }
709
32403cc3
LOK
710 int CecConfigCB(void *cbParam, const CEC::libcec_configuration &config)
711 {
712 if (g_configCB)
713 return g_configCB(config);
714 return 0;
715 }
716
6e919697 717 int CecAlertCB(void *cbParam, const CEC::libcec_alert alert, const CEC::libcec_parameter &data)
5ee37222
MK
718 {
719 if (g_alertCB)
720 return g_alertCB(alert, data);
721 return 0;
722 }
723
4ef3b314
LOK
724 #pragma managed
725 // delegates for the unmanaged callback methods
726 public delegate int CecLogMessageManagedDelegate(const CEC::cec_log_message &);
727 public delegate int CecKeyPressManagedDelegate(const CEC::cec_keypress &);
728 public delegate int CecCommandManagedDelegate(const CEC::cec_command &);
32403cc3 729 public delegate int CecConfigManagedDelegate(const CEC::libcec_configuration &);
5ee37222 730 public delegate int CecAlertManagedDelegate(const CEC::libcec_alert, const CEC::libcec_parameter &);
4ef3b314
LOK
731
732 // callback method interface
733 public ref class CecCallbackMethods
734 {
735 public:
11fea069
LOK
736 CecCallbackMethods(void)
737 {
738 m_bHasCallbacks = false;
739 m_bDelegatesCreated = false;
740 }
4ef3b314
LOK
741
742 ~CecCallbackMethods(void)
743 {
744 DestroyDelegates();
745 }
746
747 protected:
748 !CecCallbackMethods(void)
749 {
750 DestroyDelegates();
751 }
752
753 public:
dae4328c
LOK
754 virtual void DisableCallbacks(void)
755 {
11fea069 756 DestroyDelegates();
dae4328c 757 }
11fea069 758
4ef3b314
LOK
759 virtual bool EnableCallbacks(CecCallbackMethods ^ callbacks)
760 {
11fea069 761 CreateDelegates();
4ef3b314
LOK
762 if (!m_bHasCallbacks)
763 {
764 m_bHasCallbacks = true;
765 m_callbacks = callbacks;
766 return true;
767 }
768
769 return false;
770 }
771
772 virtual int ReceiveLogMessage(CecLogMessage ^ message)
773 {
774 return 0;
775 }
776
777 virtual int ReceiveKeypress(CecKeypress ^ key)
778 {
779 return 0;
780 }
781
782 virtual int ReceiveCommand(CecCommand ^ command)
783 {
784 return 0;
785 }
786
32403cc3
LOK
787 virtual int ConfigurationChanged(LibCECConfiguration ^ config)
788 {
789 return 0;
790 }
6e919697
MK
791
792 virtual int ReceiveAlert(CecAlert alert, CecParameter ^ data)
793 {
794 return 0;
795 }
796
4ef3b314
LOK
797 protected:
798 // managed callback methods
799 int CecLogMessageManaged(const CEC::cec_log_message &message)
800 {
801 int iReturn(0);
802 if (m_bHasCallbacks)
803 iReturn = m_callbacks->ReceiveLogMessage(gcnew CecLogMessage(gcnew System::String(message.message), (CecLogLevel)message.level, message.time));
804 return iReturn;
805 }
806
807 int CecKeyPressManaged(const CEC::cec_keypress &key)
808 {
809 int iReturn(0);
810 if (m_bHasCallbacks)
811 iReturn = m_callbacks->ReceiveKeypress(gcnew CecKeypress(key.keycode, key.duration));
812 return iReturn;
813 }
814
815 int CecCommandManaged(const CEC::cec_command &command)
816 {
817 int iReturn(0);
818 if (m_bHasCallbacks)
819 {
820 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);
821 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
822 newCommand->Parameters->PushBack(command.parameters[iPtr]);
823 iReturn = m_callbacks->ReceiveCommand(newCommand);
824 }
825 return iReturn;
826 }
827
32403cc3
LOK
828 int CecConfigManaged(const CEC::libcec_configuration &config)
829 {
830 int iReturn(0);
831 if (m_bHasCallbacks)
832 {
833 LibCECConfiguration ^netConfig = gcnew LibCECConfiguration();
3efda01a 834 netConfig->Update(config);
32403cc3
LOK
835 iReturn = m_callbacks->ConfigurationChanged(netConfig);
836 }
837 return iReturn;
838 }
839
6e919697
MK
840 int CecAlertManaged(const CEC::libcec_alert alert, const CEC::libcec_parameter &data)
841 {
842 int iReturn(0);
843 if (m_bHasCallbacks)
844 {
845 CecParameterType newType = (CecParameterType)data.paramType;
846 if (newType == CecParameterType::ParameterTypeString)
847 {
848 System::String ^ newData = gcnew System::String((const char *)data.paramData, 0, 128);
849 CecParameter ^ newParam = gcnew CecParameter(newType, newData);
850 iReturn = m_callbacks->ReceiveAlert((CecAlert)alert, newParam);
851 }
852 }
853 return iReturn;
854 }
855
4ef3b314
LOK
856 void DestroyDelegates()
857 {
11fea069
LOK
858 m_bHasCallbacks = false;
859 if (m_bDelegatesCreated)
006b76b9 860 {
11fea069 861 m_bDelegatesCreated = false;
006b76b9
LOK
862 m_logMessageGCHandle.Free();
863 m_keypressGCHandle.Free();
864 m_commandGCHandle.Free();
865 }
4ef3b314
LOK
866 }
867
11fea069
LOK
868 void CreateDelegates()
869 {
870 DestroyDelegates();
871
872 if (!m_bDelegatesCreated)
873 {
874 msclr::interop::marshal_context ^ context = gcnew msclr::interop::marshal_context();
875
876 // create the delegate method for the log message callback
877 m_logMessageDelegate = gcnew CecLogMessageManagedDelegate(this, &CecCallbackMethods::CecLogMessageManaged);
878 m_logMessageGCHandle = System::Runtime::InteropServices::GCHandle::Alloc(m_logMessageDelegate);
879 g_logCB = static_cast<LOGCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_logMessageDelegate).ToPointer());
880 g_cecCallbacks.CBCecLogMessage = CecLogMessageCB;
881
882 // create the delegate method for the keypress callback
883 m_keypressDelegate = gcnew CecKeyPressManagedDelegate(this, &CecCallbackMethods::CecKeyPressManaged);
884 m_keypressGCHandle = System::Runtime::InteropServices::GCHandle::Alloc(m_keypressDelegate);
885 g_keyCB = static_cast<KEYCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_keypressDelegate).ToPointer());
886 g_cecCallbacks.CBCecKeyPress = CecKeyPressCB;
887
888 // create the delegate method for the command callback
889 m_commandDelegate = gcnew CecCommandManagedDelegate(this, &CecCallbackMethods::CecCommandManaged);
890 m_commandGCHandle = System::Runtime::InteropServices::GCHandle::Alloc(m_commandDelegate);
891 g_commandCB = static_cast<COMMANDCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_commandDelegate).ToPointer());
892 g_cecCallbacks.CBCecCommand = CecCommandCB;
893
894 // create the delegate method for the configuration change callback
895 m_configDelegate = gcnew CecConfigManagedDelegate(this, &CecCallbackMethods::CecConfigManaged);
896 m_configGCHandle = System::Runtime::InteropServices::GCHandle::Alloc(m_configDelegate);
897 g_configCB = static_cast<CONFIGCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_configDelegate).ToPointer());
898 g_cecCallbacks.CBCecConfigurationChanged = CecConfigCB;
899
5ee37222
MK
900 // create the delegate method for the alert callback
901 m_alertDelegate = gcnew CecAlertManagedDelegate(this, &CecCallbackMethods::CecAlertManaged);
902 m_alertGCHandle = System::Runtime::InteropServices::GCHandle::Alloc(m_alertDelegate);
6e919697 903 g_alertCB = static_cast<ALERTCB>(System::Runtime::InteropServices::Marshal::GetFunctionPointerForDelegate(m_alertDelegate).ToPointer());
5ee37222
MK
904 g_cecCallbacks.CBCecAlert = CecAlertCB;
905
11fea069
LOK
906 delete context;
907 m_bDelegatesCreated = true;
908 }
909 }
910
4ef3b314
LOK
911 CecLogMessageManagedDelegate ^ m_logMessageDelegate;
912 static System::Runtime::InteropServices::GCHandle m_logMessageGCHandle;
913 LOGCB m_logMessageCallback;
914
915 CecKeyPressManagedDelegate ^ m_keypressDelegate;
916 static System::Runtime::InteropServices::GCHandle m_keypressGCHandle;
917 KEYCB m_keypressCallback;
918
919 CecCommandManagedDelegate ^ m_commandDelegate;
920 static System::Runtime::InteropServices::GCHandle m_commandGCHandle;
921 COMMANDCB m_commandCallback;
922
32403cc3
LOK
923 CecConfigManagedDelegate ^ m_configDelegate;
924 static System::Runtime::InteropServices::GCHandle m_configGCHandle;
925 CONFIGCB m_configCallback;
926
5ee37222
MK
927 CecAlertManagedDelegate ^ m_alertDelegate;
928 static System::Runtime::InteropServices::GCHandle m_alertGCHandle;
929 CONFIGCB m_alertCallback;
930
4ef3b314
LOK
931 CecCallbackMethods ^ m_callbacks;
932 bool m_bHasCallbacks;
11fea069 933 bool m_bDelegatesCreated;
4ef3b314 934 };
4ef3b314 935}