76dd00624e7948d3fcbf9eedfcf7d73f156f1b86
[deb_libcec.git] / src / LibCecSharp / LibCecSharp.cpp
1 /*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33 #include "CecSharpTypes.h"
34 #using <System.dll>
35
36 using namespace System;
37 using namespace System::Runtime::InteropServices;
38 using namespace CEC;
39 using namespace msclr::interop;
40
41 namespace CecSharp
42 {
43 public ref class LibCecSharp : public CecCallbackMethods
44 {
45 public:
46 LibCecSharp(LibCECConfiguration ^config)
47 {
48 m_callbacks = config->Callbacks;
49 CecCallbackMethods::EnableCallbacks(m_callbacks);
50 if (!InitialiseLibCec(config))
51 throw gcnew Exception("Could not initialise LibCecSharp");
52 }
53
54 LibCecSharp(String ^ strDeviceName, CecDeviceTypeList ^ deviceTypes)
55 {
56 m_callbacks = gcnew CecCallbackMethods();
57 LibCECConfiguration ^config = gcnew LibCECConfiguration();
58 config->SetCallbacks(this);
59 config->DeviceName = strDeviceName;
60 config->DeviceTypes = deviceTypes;
61 if (!InitialiseLibCec(config))
62 throw gcnew Exception("Could not initialise LibCecSharp");
63 }
64
65 ~LibCecSharp(void)
66 {
67 Close();
68 m_libCec = NULL;
69 }
70
71 private:
72 !LibCecSharp(void)
73 {
74 Close();
75 m_libCec = NULL;
76 }
77
78 bool InitialiseLibCec(LibCECConfiguration ^config)
79 {
80 marshal_context ^ context = gcnew marshal_context();
81 libcec_configuration libCecConfig;
82 ConvertConfiguration(context, config, libCecConfig);
83
84 m_libCec = (ICECAdapter *) CECInitialise(&libCecConfig);
85
86 delete context;
87 return m_libCec != NULL;
88 }
89
90 void ConvertConfiguration(marshal_context ^context, LibCECConfiguration ^netConfig, CEC::libcec_configuration &config)
91 {
92 config.Clear();
93
94 _snprintf_s(config.strDeviceName, 13, context->marshal_as<const char*>(netConfig->DeviceName));
95 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
96 config.deviceTypes.types[iPtr] = (cec_device_type)netConfig->DeviceTypes->Types[iPtr];
97
98 config.bAutodetectAddress = netConfig->AutodetectAddress ? 1 : 0;
99 config.iPhysicalAddress = netConfig->PhysicalAddress;
100 config.baseDevice = (cec_logical_address)netConfig->BaseDevice;
101 config.iHDMIPort = netConfig->HDMIPort;
102 config.clientVersion = (cec_client_version)netConfig->ClientVersion;
103 config.bGetSettingsFromROM = netConfig->GetSettingsFromROM ? 1 : 0;
104 config.bActivateSource = netConfig->ActivateSource ? 1 : 0;
105 config.tvVendor = (cec_vendor_id)netConfig->TvVendor;
106 config.wakeDevices.Clear();
107 for (int iPtr = 0; iPtr < 16; iPtr++)
108 {
109 if (netConfig->WakeDevices->IsSet((CecLogicalAddress)iPtr))
110 config.wakeDevices.Set((cec_logical_address)iPtr);
111 }
112 config.powerOffDevices.Clear();
113 for (int iPtr = 0; iPtr < 16; iPtr++)
114 {
115 if (netConfig->PowerOffDevices->IsSet((CecLogicalAddress)iPtr))
116 config.powerOffDevices.Set((cec_logical_address)iPtr);
117 }
118 config.bPowerOffScreensaver = netConfig->PowerOffScreensaver ? 1 : 0;
119 config.bPowerOffOnStandby = netConfig->PowerOffOnStandby ? 1 : 0;
120 config.bSendInactiveSource = netConfig->SendInactiveSource ? 1 : 0;
121 config.callbacks = &g_cecCallbacks;
122 }
123
124 public:
125 array<CecAdapter ^> ^ FindAdapters(String ^ path)
126 {
127 cec_adapter *devices = new cec_adapter[10];
128
129 marshal_context ^ context = gcnew marshal_context();
130 const char* strPathC = path->Length > 0 ? context->marshal_as<const char*>(path) : NULL;
131
132 uint8_t iDevicesFound = m_libCec->FindAdapters(devices, 10, NULL);
133
134 array<CecAdapter ^> ^ adapters = gcnew array<CecAdapter ^>(iDevicesFound);
135 for (unsigned int iPtr = 0; iPtr < iDevicesFound; iPtr++)
136 adapters[iPtr] = gcnew CecAdapter(gcnew String(devices[iPtr].path), gcnew String(devices[iPtr].comm));
137
138 delete devices;
139 delete context;
140 return adapters;
141 }
142
143 bool Open(String ^ strPort, int iTimeoutMs)
144 {
145 CecCallbackMethods::EnableCallbacks(m_callbacks);
146 EnableCallbacks(m_callbacks);
147 marshal_context ^ context = gcnew marshal_context();
148 const char* strPortC = context->marshal_as<const char*>(strPort);
149 bool bReturn = m_libCec->Open(strPortC, iTimeoutMs);
150 delete context;
151 return bReturn;
152 }
153
154 void Close(void)
155 {
156 DisableCallbacks();
157 m_libCec->Close();
158 }
159
160 virtual void DisableCallbacks(void) override
161 {
162 // delete the callbacks, since these might already have been destroyed in .NET
163 CecCallbackMethods::DisableCallbacks();
164 if (m_libCec)
165 m_libCec->EnableCallbacks(NULL, NULL);
166 }
167
168 virtual bool EnableCallbacks(CecCallbackMethods ^ callbacks) override
169 {
170 if (m_libCec && CecCallbackMethods::EnableCallbacks(callbacks))
171 return m_libCec->EnableCallbacks(NULL, &g_cecCallbacks);
172
173 return false;
174 }
175
176 bool PingAdapter(void)
177 {
178 return m_libCec->PingAdapter();
179 }
180
181 bool StartBootloader(void)
182 {
183 return m_libCec->StartBootloader();
184 }
185
186 int GetMinLibVersion(void)
187 {
188 return m_libCec->GetMinLibVersion();
189 }
190
191 int GetLibVersionMajor(void)
192 {
193 return m_libCec->GetLibVersionMajor();
194 }
195
196 int GetLibVersionMinor(void)
197 {
198 return m_libCec->GetLibVersionMinor();
199 }
200
201 CecLogMessage ^ GetNextLogMessage(void)
202 {
203 cec_log_message msg;
204 if (m_libCec->GetNextLogMessage(&msg))
205 {
206 return gcnew CecLogMessage(gcnew String(msg.message), (CecLogLevel)msg.level, msg.time);
207 }
208
209 return gcnew CecLogMessage();
210 }
211
212 CecKeypress ^ GetNextKeypress(void)
213 {
214 cec_keypress key;
215 if (m_libCec->GetNextKeypress(&key))
216 {
217 return gcnew CecKeypress(key.keycode, key.duration);
218 }
219
220 return gcnew CecKeypress();
221 }
222
223 CecCommand ^ GetNextCommand(void)
224 {
225 cec_command command;
226 if (m_libCec->GetNextCommand(&command))
227 {
228 CecCommand ^ retVal = gcnew CecCommand((CecLogicalAddress)command.initiator, (CecLogicalAddress)command.destination, command.ack == 1 ? true : false, command.eom == 1 ? true : false, (CecOpcode)command.opcode, command.transmit_timeout);
229 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
230 retVal->Parameters->PushBack(command.parameters[iPtr]);
231 return retVal;
232 }
233
234 return gcnew CecCommand();
235 }
236
237 bool Transmit(CecCommand ^ command)
238 {
239 cec_command ccommand;
240 cec_command::Format(ccommand, (cec_logical_address)command->Initiator, (cec_logical_address)command->Destination, (cec_opcode)command->Opcode);
241 ccommand.transmit_timeout = command->TransmitTimeout;
242 ccommand.eom = command->Eom;
243 ccommand.ack = command->Ack;
244 for (unsigned int iPtr = 0; iPtr < command->Parameters->Size; iPtr++)
245 ccommand.parameters.PushBack(command->Parameters->Data[iPtr]);
246
247 return m_libCec->Transmit(ccommand);
248 }
249
250 bool SetLogicalAddress(CecLogicalAddress logicalAddress)
251 {
252 return m_libCec->SetLogicalAddress((cec_logical_address) logicalAddress);
253 }
254
255 bool SetPhysicalAddress(uint16_t physicalAddress)
256 {
257 return m_libCec->SetPhysicalAddress(physicalAddress);
258 }
259
260 bool PowerOnDevices(CecLogicalAddress logicalAddress)
261 {
262 return m_libCec->PowerOnDevices((cec_logical_address) logicalAddress);
263 }
264
265 bool StandbyDevices(CecLogicalAddress logicalAddress)
266 {
267 return m_libCec->StandbyDevices((cec_logical_address) logicalAddress);
268 }
269
270 bool PollDevice(CecLogicalAddress logicalAddress)
271 {
272 return m_libCec->PollDevice((cec_logical_address) logicalAddress);
273 }
274
275 bool SetActiveSource(CecDeviceType type)
276 {
277 return m_libCec->SetActiveSource((cec_device_type) type);
278 }
279
280 bool SetDeckControlMode(CecDeckControlMode mode, bool sendUpdate)
281 {
282 return m_libCec->SetDeckControlMode((cec_deck_control_mode) mode, sendUpdate);
283 }
284
285 bool SetDeckInfo(CecDeckInfo info, bool sendUpdate)
286 {
287 return m_libCec->SetDeckInfo((cec_deck_info) info, sendUpdate);
288 }
289
290 bool SetInactiveView(void)
291 {
292 return m_libCec->SetInactiveView();
293 }
294
295 bool SetMenuState(CecMenuState state, bool sendUpdate)
296 {
297 return m_libCec->SetMenuState((cec_menu_state) state, sendUpdate);
298 }
299
300 bool SetOSDString(CecLogicalAddress logicalAddress, CecDisplayControl duration, String ^ message)
301 {
302 marshal_context ^ context = gcnew marshal_context();
303 const char* strMessageC = context->marshal_as<const char*>(message);
304
305 bool bReturn = m_libCec->SetOSDString((cec_logical_address) logicalAddress, (cec_display_control) duration, strMessageC);
306
307 delete context;
308 return bReturn;
309 }
310
311 bool SwitchMonitoring(bool enable)
312 {
313 return m_libCec->SwitchMonitoring(enable);
314 }
315
316 CecVersion GetDeviceCecVersion(CecLogicalAddress logicalAddress)
317 {
318 return (CecVersion) m_libCec->GetDeviceCecVersion((cec_logical_address) logicalAddress);
319 }
320
321 String ^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress)
322 {
323 cec_menu_language lang;
324 if (m_libCec->GetDeviceMenuLanguage((cec_logical_address) logicalAddress, &lang))
325 {
326 return gcnew String(lang.language);
327 }
328
329 return gcnew String("");
330 }
331
332 CecVendorId GetDeviceVendorId(CecLogicalAddress logicalAddress)
333 {
334 return (CecVendorId)m_libCec->GetDeviceVendorId((cec_logical_address) logicalAddress);
335 }
336
337 CecPowerStatus GetDevicePowerStatus(CecLogicalAddress logicalAddress)
338 {
339 return (CecPowerStatus) m_libCec->GetDevicePowerStatus((cec_logical_address) logicalAddress);
340 }
341
342 void RescanActiveDevices(void)
343 {
344 m_libCec->RescanActiveDevices();
345 }
346
347 CecLogicalAddresses ^ GetActiveDevices(void)
348 {
349 CecLogicalAddresses ^ retVal = gcnew CecLogicalAddresses();
350 unsigned int iDevices = 0;
351
352 cec_logical_addresses activeDevices = m_libCec->GetActiveDevices();
353
354 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
355 if (activeDevices[iPtr])
356 retVal->Addresses[iDevices++] = (CecLogicalAddress)iPtr;
357
358 return retVal;
359 }
360
361 bool IsActiveDevice(CecLogicalAddress logicalAddress)
362 {
363 return m_libCec->IsActiveDevice((cec_logical_address)logicalAddress);
364 }
365
366 bool IsActiveDeviceType(CecDeviceType type)
367 {
368 return m_libCec->IsActiveDeviceType((cec_device_type)type);
369 }
370
371 bool SetHDMIPort(CecLogicalAddress address, uint8_t port)
372 {
373 return m_libCec->SetHDMIPort((cec_logical_address)address, port);
374 }
375
376 uint8_t VolumeUp(bool wait)
377 {
378 return m_libCec->VolumeUp(wait);
379 }
380
381 uint8_t VolumeDown(bool wait)
382 {
383 return m_libCec->VolumeDown(wait);
384 }
385
386 uint8_t MuteAudio(bool wait)
387 {
388 return m_libCec->MuteAudio(wait);
389 }
390
391 bool SendKeypress(CecLogicalAddress destination, CecUserControlCode key, bool wait)
392 {
393 return m_libCec->SendKeypress((cec_logical_address)destination, (cec_user_control_code)key, wait);
394 }
395
396 bool SendKeyRelease(CecLogicalAddress destination, bool wait)
397 {
398 return m_libCec->SendKeyRelease((cec_logical_address)destination, wait);
399 }
400
401 String ^ GetDeviceOSDName(CecLogicalAddress logicalAddress)
402 {
403 cec_osd_name osd = m_libCec->GetDeviceOSDName((cec_logical_address) logicalAddress);
404 return gcnew String(osd.name);
405 }
406
407 CecLogicalAddress GetActiveSource()
408 {
409 return (CecLogicalAddress)m_libCec->GetActiveSource();
410 }
411
412 bool IsActiveSource(CecLogicalAddress logicalAddress)
413 {
414 return m_libCec->IsActiveSource((cec_logical_address)logicalAddress);
415 }
416
417 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress)
418 {
419 return m_libCec->GetDevicePhysicalAddress((cec_logical_address)iAddress);
420 }
421
422 bool SetStreamPath(CecLogicalAddress iAddress)
423 {
424 return m_libCec->SetStreamPath((cec_logical_address)iAddress);
425 }
426
427 bool SetStreamPath(uint16_t iPhysicalAddress)
428 {
429 return m_libCec->SetStreamPath(iPhysicalAddress);
430 }
431
432 CecLogicalAddresses ^GetLogicalAddresses(void)
433 {
434 CecLogicalAddresses ^addr = gcnew CecLogicalAddresses();
435 cec_logical_addresses libAddr = m_libCec->GetLogicalAddresses();
436 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
437 addr->Addresses[iPtr] = (CecLogicalAddress)libAddr.addresses[iPtr];
438 addr->Primary = (CecLogicalAddress)libAddr.primary;
439 return addr;
440 }
441
442 bool GetCurrentConfiguration(LibCECConfiguration ^configuration)
443 {
444 libcec_configuration config;
445 config.Clear();
446
447 if (m_libCec->GetCurrentConfiguration(&config))
448 {
449 configuration->Update(config);
450 return true;
451 }
452 return false;
453 }
454
455 bool CanPersistConfiguration(void)
456 {
457 return m_libCec->CanPersistConfiguration();
458 }
459
460 bool PersistConfiguration(LibCECConfiguration ^configuration)
461 {
462 marshal_context ^ context = gcnew marshal_context();
463 libcec_configuration config;
464 ConvertConfiguration(context, configuration, config);
465
466 bool bReturn = m_libCec->PersistConfiguration(&config);
467
468 delete context;
469 return bReturn;
470 }
471
472 bool SetConfiguration(LibCECConfiguration ^configuration)
473 {
474 marshal_context ^ context = gcnew marshal_context();
475 libcec_configuration config;
476 ConvertConfiguration(context, configuration, config);
477
478 bool bReturn = m_libCec->SetConfiguration(&config);
479
480 delete context;
481 return bReturn;
482 }
483
484 String ^ ToString(CecLogicalAddress iAddress)
485 {
486 const char *retVal = m_libCec->ToString((cec_logical_address)iAddress);
487 return gcnew String(retVal);
488 }
489
490 String ^ ToString(CecVendorId iVendorId)
491 {
492 const char *retVal = m_libCec->ToString((cec_vendor_id)iVendorId);
493 return gcnew String(retVal);
494 }
495
496 String ^ ToString(CecVersion iVersion)
497 {
498 const char *retVal = m_libCec->ToString((cec_version)iVersion);
499 return gcnew String(retVal);
500 }
501
502 String ^ ToString(CecPowerStatus iState)
503 {
504 const char *retVal = m_libCec->ToString((cec_power_status)iState);
505 return gcnew String(retVal);
506 }
507
508 String ^ ToString(CecMenuState iState)
509 {
510 const char *retVal = m_libCec->ToString((cec_menu_state)iState);
511 return gcnew String(retVal);
512 }
513
514 String ^ ToString(CecDeckControlMode iMode)
515 {
516 const char *retVal = m_libCec->ToString((cec_deck_control_mode)iMode);
517 return gcnew String(retVal);
518 }
519
520 String ^ ToString(CecDeckInfo status)
521 {
522 const char *retVal = m_libCec->ToString((cec_deck_info)status);
523 return gcnew String(retVal);
524 }
525
526 String ^ ToString(CecOpcode opcode)
527 {
528 const char *retVal = m_libCec->ToString((cec_opcode)opcode);
529 return gcnew String(retVal);
530 }
531
532 String ^ ToString(CecSystemAudioStatus mode)
533 {
534 const char *retVal = m_libCec->ToString((cec_system_audio_status)mode);
535 return gcnew String(retVal);
536 }
537
538 String ^ ToString(CecAudioStatus status)
539 {
540 const char *retVal = m_libCec->ToString((cec_audio_status)status);
541 return gcnew String(retVal);
542 }
543
544 String ^ ToString(CecClientVersion version)
545 {
546 const char *retVal = m_libCec->ToString((cec_client_version)version);
547 return gcnew String(retVal);
548 }
549
550 String ^ ToString(CecServerVersion version)
551 {
552 const char *retVal = m_libCec->ToString((cec_server_version)version);
553 return gcnew String(retVal);
554 }
555
556 private:
557 ICECAdapter * m_libCec;
558 CecCallbackMethods ^ m_callbacks;
559 };
560 }