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