cec: extracted CecSharpTypes.h in LibCecSharp. LibCecSharp is now in the CecSharp...
[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(String ^ strDeviceName, CecDeviceTypeList ^ deviceTypes)
47 {
48 marshal_context ^ context = gcnew marshal_context();
49 m_bHasCallbacks = false;
50 const char* strDeviceNameC = context->marshal_as<const char*>(strDeviceName);
51
52 cec_device_type_list types;
53 for (unsigned int iPtr = 0; iPtr < 5; iPtr++)
54 types.types[iPtr] = (cec_device_type)deviceTypes->Types[iPtr];
55 m_libCec = (ICECAdapter *) CECInit(strDeviceNameC, types);
56
57 delete context;
58 }
59
60 ~LibCecSharp(void)
61 {
62 Close();
63 m_libCec = NULL;
64 }
65
66 protected:
67 !LibCecSharp(void)
68 {
69 Close();
70 m_libCec = NULL;
71 }
72
73 public:
74 array<CecAdapter ^> ^ FindAdapters(String ^ path)
75 {
76 cec_adapter *devices = new cec_adapter[10];
77
78 marshal_context ^ context = gcnew marshal_context();
79 const char* strPathC = path->Length > 0 ? context->marshal_as<const char*>(path) : NULL;
80
81 uint8_t iDevicesFound = m_libCec->FindAdapters(devices, 10, NULL);
82
83 array<CecAdapter ^> ^ adapters = gcnew array<CecAdapter ^>(iDevicesFound);
84 for (unsigned int iPtr = 0; iPtr < iDevicesFound; iPtr++)
85 adapters[iPtr] = gcnew CecAdapter(gcnew String(devices[iPtr].path), gcnew String(devices[iPtr].comm));
86
87 delete devices;
88 delete context;
89 return adapters;
90 }
91
92 bool Open(String ^ strPort, int iTimeoutMs)
93 {
94 marshal_context ^ context = gcnew marshal_context();
95 const char* strPortC = context->marshal_as<const char*>(strPort);
96 bool bReturn = m_libCec->Open(strPortC, iTimeoutMs);
97 delete context;
98 return bReturn;
99 }
100
101 void Close(void)
102 {
103 m_libCec->Close();
104 }
105
106 virtual bool EnableCallbacks(CecCallbackMethods ^ callbacks) override
107 {
108 if (m_libCec && CecCallbackMethods::EnableCallbacks(callbacks))
109 return m_libCec->EnableCallbacks(NULL, &g_cecCallbacks);
110
111 return false;
112 }
113
114 bool PingAdapter(void)
115 {
116 return m_libCec->PingAdapter();
117 }
118
119 bool StartBootloader(void)
120 {
121 return m_libCec->StartBootloader();
122 }
123
124 int GetMinLibVersion(void)
125 {
126 return m_libCec->GetMinLibVersion();
127 }
128
129 int GetLibVersionMajor(void)
130 {
131 return m_libCec->GetLibVersionMajor();
132 }
133
134 int GetLibVersionMinor(void)
135 {
136 return m_libCec->GetLibVersionMinor();
137 }
138
139 CecLogMessage ^ GetNextLogMessage(void)
140 {
141 cec_log_message msg;
142 if (m_libCec->GetNextLogMessage(&msg))
143 {
144 return gcnew CecLogMessage(gcnew String(msg.message), (CecLogLevel)msg.level, msg.time);
145 }
146
147 return gcnew CecLogMessage();
148 }
149
150 CecKeypress ^ GetNextKeypress(void)
151 {
152 cec_keypress key;
153 if (m_libCec->GetNextKeypress(&key))
154 {
155 return gcnew CecKeypress(key.keycode, key.duration);
156 }
157
158 return gcnew CecKeypress();
159 }
160
161 CecCommand ^ GetNextCommand(void)
162 {
163 cec_command command;
164 if (m_libCec->GetNextCommand(&command))
165 {
166 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);
167 for (uint8_t iPtr = 0; iPtr < command.parameters.size; iPtr++)
168 retVal->Parameters->PushBack(command.parameters[iPtr]);
169 return retVal;
170 }
171
172 return gcnew CecCommand();
173 }
174
175 bool Transmit(CecCommand ^ command)
176 {
177 cec_command ccommand;
178 cec_command::Format(ccommand, (cec_logical_address)command->Initiator, (cec_logical_address)command->Destination, (cec_opcode)command->Opcode);
179 ccommand.transmit_timeout = command->TransmitTimeout;
180 ccommand.eom = command->Eom;
181 ccommand.ack = command->Ack;
182 for (unsigned int iPtr = 0; iPtr < command->Parameters->Size; iPtr++)
183 ccommand.parameters.PushBack(command->Parameters->Data[iPtr]);
184
185 return m_libCec->Transmit(ccommand);
186 }
187
188 bool SetLogicalAddress(CecLogicalAddress logicalAddress)
189 {
190 return m_libCec->SetLogicalAddress((cec_logical_address) logicalAddress);
191 }
192
193 bool SetPhysicalAddress(int16_t physicalAddress)
194 {
195 return m_libCec->SetPhysicalAddress(physicalAddress);
196 }
197
198 bool PowerOnDevices(CecLogicalAddress logicalAddress)
199 {
200 return m_libCec->PowerOnDevices((cec_logical_address) logicalAddress);
201 }
202
203 bool StandbyDevices(CecLogicalAddress logicalAddress)
204 {
205 return m_libCec->StandbyDevices((cec_logical_address) logicalAddress);
206 }
207
208 bool PollDevice(CecLogicalAddress logicalAddress)
209 {
210 return m_libCec->PollDevice((cec_logical_address) logicalAddress);
211 }
212
213 bool SetActiveSource(CecDeviceType type)
214 {
215 return m_libCec->SetActiveSource((cec_device_type) type);
216 }
217
218 bool SetDeckControlMode(CecDeckControlMode mode, bool sendUpdate)
219 {
220 return m_libCec->SetDeckControlMode((cec_deck_control_mode) mode, sendUpdate);
221 }
222
223 bool SetDeckInfo(CecDeckInfo info, bool sendUpdate)
224 {
225 return m_libCec->SetDeckInfo((cec_deck_info) info, sendUpdate);
226 }
227
228 bool SetInactiveView(void)
229 {
230 return m_libCec->SetInactiveView();
231 }
232
233 bool SetMenuState(CecMenuState state, bool sendUpdate)
234 {
235 return m_libCec->SetMenuState((cec_menu_state) state, sendUpdate);
236 }
237
238 bool SetOSDString(CecLogicalAddress logicalAddress, CecDisplayControl duration, String ^ message)
239 {
240 marshal_context ^ context = gcnew marshal_context();
241 const char* strMessageC = context->marshal_as<const char*>(message);
242
243 bool bReturn = m_libCec->SetOSDString((cec_logical_address) logicalAddress, (cec_display_control) duration, strMessageC);
244
245 delete context;
246 return bReturn;
247 }
248
249 bool SwitchMonitoring(bool enable)
250 {
251 return m_libCec->SwitchMonitoring(enable);
252 }
253
254 CecVersion GetDeviceCecVersion(CecLogicalAddress logicalAddress)
255 {
256 return (CecVersion) m_libCec->GetDeviceCecVersion((cec_logical_address) logicalAddress);
257 }
258
259 String ^ GetDeviceMenuLanguage(CecLogicalAddress logicalAddress)
260 {
261 cec_menu_language lang;
262 if (m_libCec->GetDeviceMenuLanguage((cec_logical_address) logicalAddress, &lang))
263 {
264 return gcnew String(lang.language);
265 }
266
267 return gcnew String("");
268 }
269
270 CecVendorId GetDeviceVendorId(CecLogicalAddress logicalAddress)
271 {
272 return (CecVendorId)m_libCec->GetDeviceVendorId((cec_logical_address) logicalAddress);
273 }
274
275 CecPowerStatus GetDevicePowerStatus(CecLogicalAddress logicalAddress)
276 {
277 return (CecPowerStatus) m_libCec->GetDevicePowerStatus((cec_logical_address) logicalAddress);
278 }
279
280 CecLogicalAddresses ^ GetActiveDevices(void)
281 {
282 CecLogicalAddresses ^ retVal = gcnew CecLogicalAddresses();
283 unsigned int iDevices = 0;
284
285 cec_logical_addresses activeDevices = m_libCec->GetActiveDevices();
286
287 for (uint8_t iPtr = 0; iPtr < 16; iPtr++)
288 if (activeDevices[iPtr])
289 retVal->Addresses[iDevices++] = (CecLogicalAddress)iPtr;
290
291 return retVal;
292 }
293
294 bool IsActiveDevice(CecLogicalAddress logicalAddress)
295 {
296 return m_libCec->IsActiveDevice((cec_logical_address)logicalAddress);
297 }
298
299 bool IsActiveDeviceType(CecDeviceType type)
300 {
301 return m_libCec->IsActiveDeviceType((cec_device_type)type);
302 }
303
304 bool SetHDMIPort(CecLogicalAddress address, uint8_t port)
305 {
306 return m_libCec->SetHDMIPort((cec_logical_address)address, port);
307 }
308
309 uint8_t VolumeUp(bool wait)
310 {
311 return m_libCec->VolumeUp(wait);
312 }
313
314 uint8_t VolumeDown(bool wait)
315 {
316 return m_libCec->VolumeDown(wait);
317 }
318
319 uint8_t MuteAudio(bool wait)
320 {
321 return m_libCec->MuteAudio(wait);
322 }
323
324 bool SendKeypress(CecLogicalAddress destination, CecUserControlCode key, bool wait)
325 {
326 return m_libCec->SendKeypress((cec_logical_address)destination, (cec_user_control_code)key, wait);
327 }
328
329 bool SendKeyRelease(CecLogicalAddress destination, bool wait)
330 {
331 return m_libCec->SendKeyRelease((cec_logical_address)destination, wait);
332 }
333
334 String ^ GetDeviceOSDName(CecLogicalAddress logicalAddress)
335 {
336 cec_osd_name osd = m_libCec->GetDeviceOSDName((cec_logical_address) logicalAddress);
337 return gcnew String(osd.name);
338 }
339
340 CecLogicalAddress GetActiveSource()
341 {
342 return (CecLogicalAddress)m_libCec->GetActiveSource();
343 }
344
345 bool IsActiveSource(CecLogicalAddress logicalAddress)
346 {
347 return m_libCec->IsActiveSource((cec_logical_address)logicalAddress);
348 }
349
350 uint16_t GetDevicePhysicalAddress(CecLogicalAddress iAddress)
351 {
352 return m_libCec->GetDevicePhysicalAddress((cec_logical_address)iAddress);
353 }
354
355 String ^ ToString(CecLogicalAddress iAddress)
356 {
357 const char *retVal = m_libCec->ToString((cec_logical_address)iAddress);
358 return gcnew String(retVal);
359 }
360
361 String ^ ToString(CecVendorId iVendorId)
362 {
363 const char *retVal = m_libCec->ToString((cec_vendor_id)iVendorId);
364 return gcnew String(retVal);
365 }
366
367 String ^ ToString(CecVersion iVersion)
368 {
369 const char *retVal = m_libCec->ToString((cec_version)iVersion);
370 return gcnew String(retVal);
371 }
372
373 String ^ ToString(CecPowerStatus iState)
374 {
375 const char *retVal = m_libCec->ToString((cec_power_status)iState);
376 return gcnew String(retVal);
377 }
378
379 String ^ ToString(CecMenuState iState)
380 {
381 const char *retVal = m_libCec->ToString((cec_menu_state)iState);
382 return gcnew String(retVal);
383 }
384
385 String ^ ToString(CecDeckControlMode iMode)
386 {
387 const char *retVal = m_libCec->ToString((cec_deck_control_mode)iMode);
388 return gcnew String(retVal);
389 }
390
391 String ^ ToString(CecDeckInfo status)
392 {
393 const char *retVal = m_libCec->ToString((cec_deck_info)status);
394 return gcnew String(retVal);
395 }
396
397 String ^ ToString(CecOpcode opcode)
398 {
399 const char *retVal = m_libCec->ToString((cec_opcode)opcode);
400 return gcnew String(retVal);
401 }
402
403 String ^ ToString(CecSystemAudioStatus mode)
404 {
405 const char *retVal = m_libCec->ToString((cec_system_audio_status)mode);
406 return gcnew String(retVal);
407 }
408
409 String ^ ToString(CecAudioStatus status)
410 {
411 const char *retVal = m_libCec->ToString((cec_audio_status)status);
412 return gcnew String(retVal);
413 }
414
415 private:
416 ICECAdapter * m_libCec;
417 };
418 }