cec-client: added -m/--monitor startup options, which will start a monitor-only clien...
[deb_libcec.git] / src / LibCecSharp / LibCecSharp.cpp
... / ...
CommitLineData
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
36using namespace System;
37using namespace System::Runtime::InteropServices;
38using namespace CEC;
39using namespace msclr::interop;
40
41namespace 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 const char *strDeviceName = context->marshal_as<const char*>(netConfig->DeviceName);
95 memcpy_s(config.strDeviceName, 13, strDeviceName, 13);
96 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
97 config.deviceTypes.types[iPtr] = (cec_device_type)netConfig->DeviceTypes->Types[iPtr];
98
99 config.bAutodetectAddress = netConfig->AutodetectAddress ? 1 : 0;
100 config.iPhysicalAddress = netConfig->PhysicalAddress;
101 config.baseDevice = (cec_logical_address)netConfig->BaseDevice;
102 config.iHDMIPort = netConfig->HDMIPort;
103 config.clientVersion = (cec_client_version)netConfig->ClientVersion;
104 config.bGetSettingsFromROM = netConfig->GetSettingsFromROM ? 1 : 0;
105 config.bActivateSource = netConfig->ActivateSource ? 1 : 0;
106 config.tvVendor = (cec_vendor_id)netConfig->TvVendor;
107 config.wakeDevices.Clear();
108 for (int iPtr = 0; iPtr < 16; iPtr++)
109 {
110 if (netConfig->WakeDevices->IsSet((CecLogicalAddress)iPtr))
111 config.wakeDevices.Set((cec_logical_address)iPtr);
112 }
113 config.powerOffDevices.Clear();
114 for (int iPtr = 0; iPtr < 16; iPtr++)
115 {
116 if (netConfig->PowerOffDevices->IsSet((CecLogicalAddress)iPtr))
117 config.powerOffDevices.Set((cec_logical_address)iPtr);
118 }
119 config.bPowerOffScreensaver = netConfig->PowerOffScreensaver ? 1 : 0;
120 config.bPowerOffOnStandby = netConfig->PowerOffOnStandby ? 1 : 0;
121
122 if (netConfig->ServerVersion >= CecServerVersion::Version1_5_1)
123 config.bSendInactiveSource = netConfig->SendInactiveSource ? 1 : 0;
124
125 if (netConfig->ServerVersion >= CecServerVersion::Version1_6_0)
126 {
127 config.bPowerOffDevicesOnStandby = netConfig->PowerOffDevicesOnStandby ? 1 : 0;
128 config.bShutdownOnStandby = netConfig->ShutdownOnStandby ? 1 : 0;
129 }
130
131 if (netConfig->ServerVersion >= CecServerVersion::Version1_6_2)
132 {
133 const char *strDeviceLanguage = context->marshal_as<const char*>(netConfig->DeviceLanguage);
134 memcpy_s(config.strDeviceLanguage, 3, strDeviceLanguage, 3);
135 }
136
137 config.callbacks = &g_cecCallbacks;
138 }
139
140 public:
141 array<CecAdapter ^> ^ FindAdapters(String ^ path)
142 {
143 cec_adapter *devices = new cec_adapter[10];
144
145 marshal_context ^ context = gcnew marshal_context();
146 const char* strPathC = path->Length > 0 ? context->marshal_as<const char*>(path) : NULL;
147
148 uint8_t iDevicesFound = m_libCec->FindAdapters(devices, 10, NULL);
149
150 array<CecAdapter ^> ^ adapters = gcnew array<CecAdapter ^>(iDevicesFound);
151 for (unsigned int iPtr = 0; iPtr < iDevicesFound; iPtr++)
152 adapters[iPtr] = gcnew CecAdapter(gcnew String(devices[iPtr].path), gcnew String(devices[iPtr].comm));
153
154 delete devices;
155 delete context;
156 return adapters;
157 }
158
159 bool Open(String ^ strPort, int iTimeoutMs)
160 {
161 CecCallbackMethods::EnableCallbacks(m_callbacks);
162 EnableCallbacks(m_callbacks);
163 marshal_context ^ context = gcnew marshal_context();
164 const char* strPortC = context->marshal_as<const char*>(strPort);
165 bool bReturn = m_libCec->Open(strPortC, iTimeoutMs);
166 delete context;
167 return bReturn;
168 }
169
170 void Close(void)
171 {
172 DisableCallbacks();
173 m_libCec->Close();
174 }
175
176 virtual void DisableCallbacks(void) override
177 {
178 // delete the callbacks, since these might already have been destroyed in .NET
179 CecCallbackMethods::DisableCallbacks();
180 if (m_libCec)
181 m_libCec->EnableCallbacks(NULL, NULL);
182 }
183
184 virtual bool EnableCallbacks(CecCallbackMethods ^ callbacks) override
185 {
186 if (m_libCec && CecCallbackMethods::EnableCallbacks(callbacks))
187 return m_libCec->EnableCallbacks(NULL, &g_cecCallbacks);
188
189 return false;
190 }
191
192 bool PingAdapter(void)
193 {
194 return m_libCec->PingAdapter();
195 }
196
197 bool StartBootloader(void)
198 {
199 return m_libCec->StartBootloader();
200 }
201
202 int GetMinLibVersion(void)
203 {
204 return m_libCec->GetMinLibVersion();
205 }
206
207 int GetLibVersionMajor(void)
208 {
209 return m_libCec->GetLibVersionMajor();
210 }
211
212 int GetLibVersionMinor(void)
213 {
214 return m_libCec->GetLibVersionMinor();
215 }
216
217 CecLogMessage ^ GetNextLogMessage(void)
218 {
219 cec_log_message msg;
220 if (m_libCec->GetNextLogMessage(&msg))
221 {
222 return gcnew CecLogMessage(gcnew String(msg.message), (CecLogLevel)msg.level, msg.time);
223 }
224
225 return gcnew CecLogMessage();
226 }
227
228 CecKeypress ^ GetNextKeypress(void)
229 {
230 cec_keypress key;
231 if (m_libCec->GetNextKeypress(&key))
232 {
233 return gcnew CecKeypress(key.keycode, key.duration);
234 }
235
236 return gcnew CecKeypress();
237 }
238
239 CecCommand ^ GetNextCommand(void)
240 {
241 cec_command command;
242 if (m_libCec->GetNextCommand(&command))
243 {
244 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);
245 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
246 retVal->Parameters->PushBack(command.parameters[iPtr]);
247 return retVal;
248 }
249
250 return gcnew CecCommand();
251 }
252
253 bool Transmit(CecCommand ^ command)
254 {
255 cec_command ccommand;
256 cec_command::Format(ccommand, (cec_logical_address)command->Initiator, (cec_logical_address)command->Destination, (cec_opcode)command->Opcode);
257 ccommand.transmit_timeout = command->TransmitTimeout;
258 ccommand.eom = command->Eom;
259 ccommand.ack = command->Ack;
260 for (unsigned int iPtr = 0; iPtr < command->Parameters->Size; iPtr++)
261 ccommand.parameters.PushBack(command->Parameters->Data[iPtr]);
262
263 return m_libCec->Transmit(ccommand);
264 }
265
266 bool SetLogicalAddress(CecLogicalAddress logicalAddress)
267 {
268 return m_libCec->SetLogicalAddress((cec_logical_address) logicalAddress);
269 }
270
271 bool SetPhysicalAddress(uint16_t physicalAddress)
272 {
273 return m_libCec->SetPhysicalAddress(physicalAddress);
274 }
275
276 bool PowerOnDevices(CecLogicalAddress logicalAddress)
277 {
278 return m_libCec->PowerOnDevices((cec_logical_address) logicalAddress);
279 }
280
281 bool StandbyDevices(CecLogicalAddress logicalAddress)
282 {
283 return m_libCec->StandbyDevices((cec_logical_address) logicalAddress);
284 }
285
286 bool PollDevice(CecLogicalAddress logicalAddress)
287 {
288 return m_libCec->PollDevice((cec_logical_address) logicalAddress);
289 }
290
291 bool SetActiveSource(CecDeviceType type)
292 {
293 return m_libCec->SetActiveSource((cec_device_type) type);
294 }
295
296 bool SetDeckControlMode(CecDeckControlMode mode, bool sendUpdate)
297 {
298 return m_libCec->SetDeckControlMode((cec_deck_control_mode) mode, sendUpdate);
299 }
300
301 bool SetDeckInfo(CecDeckInfo info, bool sendUpdate)
302 {
303 return m_libCec->SetDeckInfo((cec_deck_info) info, sendUpdate);
304 }
305
306 bool SetInactiveView(void)
307 {
308 return m_libCec->SetInactiveView();
309 }
310
311 bool SetMenuState(CecMenuState state, bool sendUpdate)
312 {
313 return m_libCec->SetMenuState((cec_menu_state) state, sendUpdate);
314 }
315
316 bool SetOSDString(CecLogicalAddress logicalAddress, CecDisplayControl duration, String ^ message)
317 {
318 marshal_context ^ context = gcnew marshal_context();
319 const char* strMessageC = context->marshal_as<const char*>(message);
320
321 bool bReturn = m_libCec->SetOSDString((cec_logical_address) logicalAddress, (cec_display_control) duration, strMessageC);
322
323 delete context;
324 return bReturn;
325 }
326
327 bool SwitchMonitoring(bool enable)
328 {
329 return m_libCec->SwitchMonitoring(enable);
330 }
331
332 CecVersion GetDeviceCecVersion(CecLogicalAddress logicalAddress)
333 {
334 return (CecVersion) m_libCec->GetDeviceCecVersion((cec_logical_address) logicalAddress);
335 }
336
337 String ^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress)
338 {
339 cec_menu_language lang;
340 if (m_libCec->GetDeviceMenuLanguage((cec_logical_address) logicalAddress, &lang))
341 {
342 return gcnew String(lang.language);
343 }
344
345 return gcnew String("");
346 }
347
348 CecVendorId GetDeviceVendorId(CecLogicalAddress logicalAddress)
349 {
350 return (CecVendorId)m_libCec->GetDeviceVendorId((cec_logical_address) logicalAddress);
351 }
352
353 CecPowerStatus GetDevicePowerStatus(CecLogicalAddress logicalAddress)
354 {
355 return (CecPowerStatus) m_libCec->GetDevicePowerStatus((cec_logical_address) logicalAddress);
356 }
357
358 void RescanActiveDevices(void)
359 {
360 m_libCec->RescanActiveDevices();
361 }
362
363 CecLogicalAddresses ^ GetActiveDevices(void)
364 {
365 CecLogicalAddresses ^ retVal = gcnew CecLogicalAddresses();
366 unsigned int iDevices = 0;
367
368 cec_logical_addresses activeDevices = m_libCec->GetActiveDevices();
369
370 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
371 if (activeDevices[iPtr])
372 retVal->Addresses[iDevices++] = (CecLogicalAddress)iPtr;
373
374 return retVal;
375 }
376
377 bool IsActiveDevice(CecLogicalAddress logicalAddress)
378 {
379 return m_libCec->IsActiveDevice((cec_logical_address)logicalAddress);
380 }
381
382 bool IsActiveDeviceType(CecDeviceType type)
383 {
384 return m_libCec->IsActiveDeviceType((cec_device_type)type);
385 }
386
387 bool SetHDMIPort(CecLogicalAddress address, uint8_t port)
388 {
389 return m_libCec->SetHDMIPort((cec_logical_address)address, port);
390 }
391
392 uint8_t VolumeUp(bool wait)
393 {
394 return m_libCec->VolumeUp(wait);
395 }
396
397 uint8_t VolumeDown(bool wait)
398 {
399 return m_libCec->VolumeDown(wait);
400 }
401
402 uint8_t MuteAudio(bool wait)
403 {
404 return m_libCec->MuteAudio(wait);
405 }
406
407 bool SendKeypress(CecLogicalAddress destination, CecUserControlCode key, bool wait)
408 {
409 return m_libCec->SendKeypress((cec_logical_address)destination, (cec_user_control_code)key, wait);
410 }
411
412 bool SendKeyRelease(CecLogicalAddress destination, bool wait)
413 {
414 return m_libCec->SendKeyRelease((cec_logical_address)destination, wait);
415 }
416
417 String ^ GetDeviceOSDName(CecLogicalAddress logicalAddress)
418 {
419 cec_osd_name osd = m_libCec->GetDeviceOSDName((cec_logical_address) logicalAddress);
420 return gcnew String(osd.name);
421 }
422
423 CecLogicalAddress GetActiveSource()
424 {
425 return (CecLogicalAddress)m_libCec->GetActiveSource();
426 }
427
428 bool IsActiveSource(CecLogicalAddress logicalAddress)
429 {
430 return m_libCec->IsActiveSource((cec_logical_address)logicalAddress);
431 }
432
433 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress)
434 {
435 return m_libCec->GetDevicePhysicalAddress((cec_logical_address)iAddress);
436 }
437
438 bool SetStreamPath(CecLogicalAddress iAddress)
439 {
440 return m_libCec->SetStreamPath((cec_logical_address)iAddress);
441 }
442
443 bool SetStreamPath(uint16_t iPhysicalAddress)
444 {
445 return m_libCec->SetStreamPath(iPhysicalAddress);
446 }
447
448 CecLogicalAddresses ^GetLogicalAddresses(void)
449 {
450 CecLogicalAddresses ^addr = gcnew CecLogicalAddresses();
451 cec_logical_addresses libAddr = m_libCec->GetLogicalAddresses();
452 for (unsigned int iPtr = 0; iPtr < 16; iPtr++)
453 addr->Addresses[iPtr] = (CecLogicalAddress)libAddr.addresses[iPtr];
454 addr->Primary = (CecLogicalAddress)libAddr.primary;
455 return addr;
456 }
457
458 bool GetCurrentConfiguration(LibCECConfiguration ^configuration)
459 {
460 libcec_configuration config;
461 config.Clear();
462
463 if (m_libCec->GetCurrentConfiguration(&config))
464 {
465 configuration->Update(config);
466 return true;
467 }
468 return false;
469 }
470
471 bool CanPersistConfiguration(void)
472 {
473 return m_libCec->CanPersistConfiguration();
474 }
475
476 bool PersistConfiguration(LibCECConfiguration ^configuration)
477 {
478 marshal_context ^ context = gcnew marshal_context();
479 libcec_configuration config;
480 ConvertConfiguration(context, configuration, config);
481
482 bool bReturn = m_libCec->PersistConfiguration(&config);
483
484 delete context;
485 return bReturn;
486 }
487
488 bool SetConfiguration(LibCECConfiguration ^configuration)
489 {
490 marshal_context ^ context = gcnew marshal_context();
491 libcec_configuration config;
492 ConvertConfiguration(context, configuration, config);
493
494 bool bReturn = m_libCec->SetConfiguration(&config);
495
496 delete context;
497 return bReturn;
498 }
499
500 bool IsLibCECActiveSource()
501 {
502 return m_libCec->IsLibCECActiveSource();
503 }
504
505 bool GetDeviceInformation(String ^ port, LibCECConfiguration ^configuration, uint32_t timeoutMs)
506 {
507 bool bReturn(false);
508 marshal_context ^ context = gcnew marshal_context();
509
510 libcec_configuration config;
511 config.Clear();
512
513 const char* strPortC = port->Length > 0 ? context->marshal_as<const char*>(port) : NULL;
514
515 if (m_libCec->GetDeviceInformation(strPortC, &config, timeoutMs))
516 {
517 configuration->Update(config);
518 bReturn = true;
519 }
520
521 delete context;
522 return bReturn;
523 }
524
525 String ^ ToString(CecLogicalAddress iAddress)
526 {
527 const char *retVal = m_libCec->ToString((cec_logical_address)iAddress);
528 return gcnew String(retVal);
529 }
530
531 String ^ ToString(CecVendorId iVendorId)
532 {
533 const char *retVal = m_libCec->ToString((cec_vendor_id)iVendorId);
534 return gcnew String(retVal);
535 }
536
537 String ^ ToString(CecVersion iVersion)
538 {
539 const char *retVal = m_libCec->ToString((cec_version)iVersion);
540 return gcnew String(retVal);
541 }
542
543 String ^ ToString(CecPowerStatus iState)
544 {
545 const char *retVal = m_libCec->ToString((cec_power_status)iState);
546 return gcnew String(retVal);
547 }
548
549 String ^ ToString(CecMenuState iState)
550 {
551 const char *retVal = m_libCec->ToString((cec_menu_state)iState);
552 return gcnew String(retVal);
553 }
554
555 String ^ ToString(CecDeckControlMode iMode)
556 {
557 const char *retVal = m_libCec->ToString((cec_deck_control_mode)iMode);
558 return gcnew String(retVal);
559 }
560
561 String ^ ToString(CecDeckInfo status)
562 {
563 const char *retVal = m_libCec->ToString((cec_deck_info)status);
564 return gcnew String(retVal);
565 }
566
567 String ^ ToString(CecOpcode opcode)
568 {
569 const char *retVal = m_libCec->ToString((cec_opcode)opcode);
570 return gcnew String(retVal);
571 }
572
573 String ^ ToString(CecSystemAudioStatus mode)
574 {
575 const char *retVal = m_libCec->ToString((cec_system_audio_status)mode);
576 return gcnew String(retVal);
577 }
578
579 String ^ ToString(CecAudioStatus status)
580 {
581 const char *retVal = m_libCec->ToString((cec_audio_status)status);
582 return gcnew String(retVal);
583 }
584
585 String ^ ToString(CecClientVersion version)
586 {
587 const char *retVal = m_libCec->ToString((cec_client_version)version);
588 return gcnew String(retVal);
589 }
590
591 String ^ ToString(CecServerVersion version)
592 {
593 const char *retVal = m_libCec->ToString((cec_server_version)version);
594 return gcnew String(retVal);
595 }
596
597 private:
598 ICECAdapter * m_libCec;
599 CecCallbackMethods ^ m_callbacks;
600 };
601}