commit d365eb62e4c85522f43ae6776bbdb3f93676a80e Author: gered Date: Tue May 29 11:06:41 2012 -0400 add code from commit 8644 from codeplex sources diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..a664be3 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,49 @@ +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain + +*.jpg binary +*.png binary +*.gif binary + +*.cs text=auto diff=csharp +*.vb text=auto +*.c text=auto +*.cpp text=auto +*.cxx text=auto +*.h text=auto +*.hxx text=auto +*.py text=auto +*.rb text=auto +*.java text=auto +*.html text=auto +*.htm text=auto +*.css text=auto +*.scss text=auto +*.sass text=auto +*.less text=auto +*.js text=auto +*.lisp text=auto +*.clj text=auto +*.sql text=auto +*.php text=auto +*.lua text=auto +*.m text=auto +*.asm text=auto +*.erl text=auto +*.fs text=auto +*.fsx text=auto +*.hs text=auto + +*.csproj text=auto merge=union +*.vbproj text=auto merge=union +*.fsproj text=auto merge=union +*.dbproj text=auto merge=union +*.sln text=auto eol=crlf merge=union diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa8811c --- /dev/null +++ b/.gitignore @@ -0,0 +1,165 @@ + +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/** +tmp/** +tmp/**/* +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results +**/[Dd]ebug/ +**/[Rr]elease/ +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.vspscc +.builds +**/*.dotCover + +## TODO: If you have NuGet Package Restore enabled, uncomment this +#**/packages/ + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf + +# Visual Studio profiler +*.psess +*.vsp + +# ReSharper is a .NET coding add-in +_ReSharper* + +# Installshield output folder +[Ee]xpress + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish + +# Others +[Bb]in +[Oo]bj +sql +TestResults +*.Cache +ClientBin +stylecop.* +~$* +*.dbmdl +Generated_Code #added for RIA/Silverlight projects + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML + + + +############ +## Windows +############ + +# Windows image file caches +Thumbs.db + +# Folder config file +Desktop.ini + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist +build +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg + +# Mac crap +.DS_Store diff --git a/AppleWirelessKeyboard.sln b/AppleWirelessKeyboard.sln new file mode 100644 index 0000000..6abc3e9 --- /dev/null +++ b/AppleWirelessKeyboard.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppleWirelessKeyboard", "AppleWirelessKeyboard\AppleWirelessKeyboard.csproj", "{9654B149-302A-4392-8F91-4202FA6E0AE4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x86 = Debug|x86 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9654B149-302A-4392-8F91-4202FA6E0AE4}.Debug|x86.ActiveCfg = Debug|x86 + {9654B149-302A-4392-8F91-4202FA6E0AE4}.Debug|x86.Build.0 = Debug|x86 + {9654B149-302A-4392-8F91-4202FA6E0AE4}.Release|x86.ActiveCfg = Release|x86 + {9654B149-302A-4392-8F91-4202FA6E0AE4}.Release|x86.Build.0 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/AppleWirelessKeyboard/App.xaml b/AppleWirelessKeyboard/App.xaml new file mode 100644 index 0000000..42ccf36 --- /dev/null +++ b/AppleWirelessKeyboard/App.xaml @@ -0,0 +1,4 @@ + diff --git a/AppleWirelessKeyboard/App.xaml.cs b/AppleWirelessKeyboard/App.xaml.cs new file mode 100644 index 0000000..33ef59e --- /dev/null +++ b/AppleWirelessKeyboard/App.xaml.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Data; +using System.Linq; +using System.Windows; +using System.Windows.Input; +using System.Diagnostics; +using System.Timers; +using System.Threading; + +namespace AppleWirelessKeyboard +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + public static MainWindow Window { get; set; } + + private void Application_Startup(object sender, StartupEventArgs e) + { + TrayIcon.Show(); + uxsoft.VersionCheck(); + + if (!StartupShortcut.IsRegistered) + StartupShortcut.Register(); + + if (Process.GetProcessesByName(Application.ResourceAssembly.GetName().Name).Length > 1) + { + var processes = Process.GetProcessesByName(Application.ResourceAssembly.GetName().Name); + foreach (Process p in processes) + if (p != Process.GetCurrentProcess()) + p.Kill(); + } + + Window = new MainWindow(); + + AppleKeyboardHID2.Start(); + AppleKeyboardHID2.KeyDown += new AppleKeyboardHID2.AppleHIDKeyboardEventHandler(AppleKeyboardHID_KeyDown); + AppleKeyboardHID2.KeyUp += new AppleKeyboardHID2.AppleHIDKeyboardEventHandler(AppleKeyboardHID_KeyUp); + + KeyboardListener.HookedKeys.Add(Key.F2); + KeyboardListener.HookedKeys.Add(Key.F3); + KeyboardListener.HookedKeys.Add(Key.F4); + KeyboardListener.HookedKeys.Add(Key.F7); + KeyboardListener.HookedKeys.Add(Key.F8); + KeyboardListener.HookedKeys.Add(Key.F9); + KeyboardListener.HookedKeys.Add(Key.F10); + KeyboardListener.HookedKeys.Add(Key.F11); + KeyboardListener.HookedKeys.Add(Key.F12); + KeyboardListener.HookedKeys.Add(Key.Back); + KeyboardListener.HookedKeys.Add(Key.Up); + KeyboardListener.HookedKeys.Add(Key.Left); + KeyboardListener.HookedKeys.Add(Key.Right); + KeyboardListener.HookedKeys.Add(Key.Down); + KeyboardListener.HookedKeys.Add(Key.Enter); + KeyboardListener.Register(); + KeyboardListener.KeyDown += new KeyboardListener.KeyHookEventHandler(KeyboardListener_KeyDown); + + Microsoft.Win32.SystemEvents.PowerModeChanged += new Microsoft.Win32.PowerModeChangedEventHandler(SystemEvents_PowerModeChanged); + } + + void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e) + { + if (e.Mode == Microsoft.Win32.PowerModes.Resume) + { + Thread.Sleep(16000); + AppleKeyboardHID2.Shutdown(); + AppleKeyboardHID2.Start(); + } + } + + void AppleKeyboardHID_KeyUp(AppleKeyboardSpecialKeys key) + { + if (key == AppleKeyboardSpecialKeys.Fn) + KeyboardListener.ModifierFn = false; + } + + void AppleKeyboardHID_KeyDown(AppleKeyboardSpecialKeys key) + { + switch (key) + { + case AppleKeyboardSpecialKeys.Fn: + KeyboardListener.ModifierFn = true; + break; + case AppleKeyboardSpecialKeys.Eject: + KeyboardHandler.HandleEject(); + break; + } + } + + bool KeyboardListener_KeyDown(KeyboardListener.KeyHookEventArgs e) + { + return KeyboardHandler.HandleKeyDown(e); + } + + private void Application_Exit(object sender, ExitEventArgs e) + { + KeyboardListener.UnRegister(); + AppleKeyboardHID2.Shutdown(); + } + } +} diff --git a/AppleWirelessKeyboard/AppleKeyboardHID2.cs b/AppleWirelessKeyboard/AppleKeyboardHID2.cs new file mode 100644 index 0000000..60795c5 --- /dev/null +++ b/AppleWirelessKeyboard/AppleKeyboardHID2.cs @@ -0,0 +1,227 @@ +using System; +using System.IO; +using AppleWirelessKeyboard; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; +using System.Windows.Forms; +using System.Diagnostics; +using System.Threading; + +namespace AppleWirelessKeyboard +{ + public class AppleKeyboardHID2 + { + // Fields + static Stream _stream; + static bool CurrentPowerButtonIsDown; + public const int VIDApple = 0x5ac; + + // Events + public static event EventHandler Disconnected; + public delegate void AppleHIDKeyboardEventHandler(AppleKeyboardSpecialKeys key); + public static event AppleHIDKeyboardEventHandler KeyUp; + public static event AppleHIDKeyboardEventHandler KeyDown; + + //Properties + public static bool FnDown { get; set; } + public static bool EjectDown { get; set; } + public static bool PowerButtonDown { get; set; } + public static bool Registered + { + get { return _stream != null; } + } + + public static void Shutdown() + { + if (_stream != null) + { + _stream.Close(); + _stream = null; + } + } + + private static void SpecialKeyStateChanged(IAsyncResult ar) + { + if ((_stream != null) && ar.IsCompleted) + { + try + { + _stream.EndRead(ar); + } + catch (OperationCanceledException) + { + } + catch (IOException) + { + if (Disconnected != null) + { + Disconnected(null, EventArgs.Empty); + } + return; + } + byte[] asyncState = ar.AsyncState as byte[]; + byte[] buffer2 = asyncState; + for (int i = 0; i < buffer2.Length; i++) + { + byte num1 = buffer2[i]; + } + if (asyncState[0] == 0x11) + { + switch (asyncState[1]) + { + case 24: + { + EjectDown = true; + FnDown = true; + if (KeyDown != null) + KeyDown(AppleKeyboardSpecialKeys.Eject); + } + break; + case 16: + { + if (EjectDown) + { + EjectDown = false; + if (KeyUp != null) + KeyUp(AppleKeyboardSpecialKeys.Eject); + } + FnDown = true; + if (KeyDown != null) + KeyDown(AppleKeyboardSpecialKeys.Fn); + } + break; + case 8: + { + if (FnDown) + { + FnDown = false; + if (KeyUp != null) + KeyUp(AppleKeyboardSpecialKeys.Fn); + } + EjectDown = true; + if (KeyDown != null) + KeyDown(AppleKeyboardSpecialKeys.Eject); + } + break; + case 0: + { + if (EjectDown) + { + EjectDown = false; + if (KeyUp != null) + KeyUp(AppleKeyboardSpecialKeys.Eject); + } + if (FnDown) + { + FnDown = false; + if (KeyUp != null) + KeyUp(AppleKeyboardSpecialKeys.Fn); + } + } + break; + } + } + else if (asyncState[0] == 0x13) + { + CurrentPowerButtonIsDown = asyncState[1] == 1; + } + _stream.BeginRead(asyncState, 0, asyncState.Length, new AsyncCallback(SpecialKeyStateChanged), asyncState); + } + } + + public static bool Start() + { + Guid guid; + HIDImports.SP_DEVICE_INTERFACE_DATA sp_device_interface_data = new HIDImports.SP_DEVICE_INTERFACE_DATA() { cbSize = Marshal.SizeOf(typeof(HIDImports.SP_DEVICE_INTERFACE_DATA)) }; + + if (_stream != null) + { + throw new InvalidOperationException("No Stream!"); + } + + HIDImports.HidD_GetHidGuid(out guid); + IntPtr hDevInfo = HIDImports.SetupDiGetClassDevs(ref guid, null, IntPtr.Zero, 0x10); + + int num = 0; + while (HIDImports.SetupDiEnumDeviceInterfaces(hDevInfo, IntPtr.Zero, ref guid, num++, ref sp_device_interface_data)) + { + uint num2; + HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData = new HIDImports.SP_DEVICE_INTERFACE_DETAIL_DATA { cbSize = (IntPtr.Size == 8) ? (uint)8 : (uint)5 }; + + HIDImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref sp_device_interface_data, IntPtr.Zero, 0, out num2, IntPtr.Zero); + if (HIDImports.SetupDiGetDeviceInterfaceDetail(hDevInfo, ref sp_device_interface_data, ref deviceInterfaceDetailData, num2, out num2, IntPtr.Zero)) + { + HIDImports.HIDD_ATTRIBUTES hidd_attributes = new HIDImports.HIDD_ATTRIBUTES() { Size = Marshal.SizeOf(typeof(HIDImports.HIDD_ATTRIBUTES)) }; + + SafeFileHandle handle = HIDImports.CreateFile(deviceInterfaceDetailData.DevicePath, FileAccess.ReadWrite, FileShare.ReadWrite, IntPtr.Zero, FileMode.Open, HIDImports.EFileAttributes.Overlapped, IntPtr.Zero); + + if (HIDImports.HidD_GetAttributes(handle.DangerousGetHandle(), ref hidd_attributes)) + { + if (IsAppleWirelessKeyboard(hidd_attributes.VendorID, hidd_attributes.ProductID)) + { + _stream = new FileStream(handle, FileAccess.ReadWrite, 0x16, true); + //break; + } + else + { + handle.Close(); + } + } + } + } + + HIDImports.SetupDiDestroyDeviceInfoList(hDevInfo); + + if (_stream != null) + { + byte[] buffer = new byte[0x16]; + _stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(SpecialKeyStateChanged), buffer); + return true; + } + return false; + } + + private static bool IsAppleWirelessKeyboard(int vid, int pid) + { + if (vid == VIDApple) + { + Guid HIDGUID; + HIDImports.HidD_GetHidGuid(out HIDGUID); + + IntPtr DeviceInfo = HIDImports.SetupDiGetClassDevs(ref HIDGUID, null, IntPtr.Zero, 16); + + uint MemberIndex = 0; + + HIDImports.SP_DEVINFO_DATA DID = new HIDImports.SP_DEVINFO_DATA() { cbSize = (uint)Marshal.SizeOf(typeof(HIDImports.SP_DEVINFO_DATA)) }; + + while (HIDImports.SetupDiEnumDeviceInfo(DeviceInfo, MemberIndex++, ref DID)) + { + uint RequiredSize = 0; + uint PropertyDataType = 0; + IntPtr buffer = Marshal.AllocHGlobal(512); + string CLASS = ""; + + + if (HIDImports.SetupDiGetDeviceRegistryProperty(DeviceInfo, ref DID, (uint)HIDImports.SPDRP.SPDRP_CLASS, out PropertyDataType, buffer, 512, out RequiredSize)) + CLASS = Marshal.PtrToStringAuto(buffer); + + if (CLASS.Equals("Keyboard", StringComparison.InvariantCultureIgnoreCase)) + return true; + } + + HIDImports.SetupDiDestroyDeviceInfoList(DeviceInfo); + + return true; + } + else return false; + } + } + public enum AppleKeyboardSpecialKeys : byte + { + //Fn + Eject = 24 + Fn = 16, + Eject = 8, + PowerButton = 3 + //None = 0 + } +} \ No newline at end of file diff --git a/AppleWirelessKeyboard/AppleWirelessKeyboard.csproj b/AppleWirelessKeyboard/AppleWirelessKeyboard.csproj new file mode 100644 index 0000000..901b2c3 --- /dev/null +++ b/AppleWirelessKeyboard/AppleWirelessKeyboard.csproj @@ -0,0 +1,358 @@ + + + + Debug + x86 + 8.0.30703 + 2.0 + {9654B149-302A-4392-8F91-4202FA6E0AE4} + WinExe + Properties + AppleWirelessKeyboard + AppleWirelessKeyboard + v4.0 + Client + 512 + {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 4 + true + OnOutputUpdated + + + + + + + + + publish\ + true + Web + true + Background + 7 + Days + false + false + true + http://applewirelesskbrd.codeplex.com/releases/clickonce/ + http://applewirelesskbrd.codeplex.com/releases/clickonce/ + http://applewirelesskbrd.codeplex.com + AppleWirelessKeyboard + X-soft + AppleWirelessKeyboard + true + publish.htm + false + true + 0 + 2.1.0.0 + false + true + true + + + AnyCPU + true + full + true + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + 902A841B3FDD641CBAF24748EC3D69DFF5ECCF12 + + + AppleWirelessKeyboard_TemporaryKey.pfx + + + true + + + true + + + Gnome-Preferences-Desktop-Keyboard-Shortcuts.ico + + + AppleWirelessKeyboard.App + + + + + + + + + + + + + + + 4.0 + + + + + + + + MSBuild:Compile + Designer + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Eject.xaml + + + MuteOff.xaml + + + MuteOn.xaml + + + Next.xaml + + + Off.xaml + + + On.xaml + + + PlayPause.xaml + + + Previous.xaml + + + PrintScreen.xaml + + + TaskManager.xaml + + + VolumeOff.xaml + + + VolumeOn.xaml + + + + + + + + + + + PowerStatusBox.xaml + + + + + + Configuration.xaml + + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + MSBuild:Compile + Designer + + + App.xaml + Code + + + MainWindow.xaml + Code + + + Designer + MSBuild:Compile + + + Designer + MSBuild:Compile + + + + + Code + + + True + True + Resources.resx + + + True + Settings.settings + True + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + + + + False + Microsoft .NET Framework 4 Client Profile %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + + + False + Windows Installer 3.1 + true + + + + + + + + {9E93C96F-CF0D-43F6-8BA8-B807A3370712} + 1 + 13 + 0 + tlbimp + False + True + + + + + + if /I "$(ConfigurationName)" == "Release" Eazfuscator.NET.exe "$(TargetPath)" --msbuild-project-path "$(ProjectPath)" --msbuild-project-configuration "$(ConfigurationName)" --msbuild-project-platform "$(PlatformName)" -n --newline-flush -v 2.9 + + \ No newline at end of file diff --git a/AppleWirelessKeyboard/AppleWirelessKeyboard_TemporaryKey.pfx b/AppleWirelessKeyboard/AppleWirelessKeyboard_TemporaryKey.pfx new file mode 100644 index 0000000..2a18f54 Binary files /dev/null and b/AppleWirelessKeyboard/AppleWirelessKeyboard_TemporaryKey.pfx differ diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AUDIO_VOLUME_NOTIFICATION_DATA.cs b/AppleWirelessKeyboard/CoreAudioAPI/AUDIO_VOLUME_NOTIFICATION_DATA.cs new file mode 100644 index 0000000..fe3319d --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AUDIO_VOLUME_NOTIFICATION_DATA.cs @@ -0,0 +1,54 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace CoreAudioApi.Interfaces +{ + internal struct AUDIO_VOLUME_NOTIFICATION_DATA + { + public Guid guidEventContext; + public bool bMuted; + public float fMasterVolume; + public uint nChannels; + public float ChannelVolume; + + //Code Should Compile at warning level4 without any warnings, + //However this struct will give us Warning CS0649: Field [Fieldname] + //is never assigned to, and will always have its default value + //You can disable CS0649 in the project options but that will disable + //the warning for the whole project, it's a nice warning and we do want + //it in other places so we make a nice dummy function to keep the compiler + //happy. + private void FixCS0649() + { + guidEventContext = Guid.Empty; + bMuted = false; + fMasterVolume = 0; + nChannels = 0; + ChannelVolume = 0; + } + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioEndPointVolumeVolumeRange.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndPointVolumeVolumeRange.cs new file mode 100644 index 0000000..73dc0f4 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndPointVolumeVolumeRange.cs @@ -0,0 +1,65 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + public class AudioEndPointVolumeVolumeRange + { + float _VolumeMindB; + float _VolumeMaxdB; + float _VolumeIncrementdB; + + internal AudioEndPointVolumeVolumeRange(IAudioEndpointVolume parent) + { + Marshal.ThrowExceptionForHR(parent.GetVolumeRange(out _VolumeMindB,out _VolumeMaxdB,out _VolumeIncrementdB)); + } + + public float MindB + { + get + { + return _VolumeMindB; + } + } + + public float MaxdB + { + get + { + return _VolumeMaxdB; + } + } + + public float IncrementdB + { + get + { + return _VolumeIncrementdB; + } + } + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolume.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolume.cs new file mode 100644 index 0000000..2d2ce21 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolume.cs @@ -0,0 +1,157 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System; +using System.Collections.Generic; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + + public class AudioEndpointVolume : IDisposable + { + private IAudioEndpointVolume _AudioEndPointVolume; + private AudioEndpointVolumeChannels _Channels; + private AudioEndpointVolumeStepInformation _StepInformation; + private AudioEndPointVolumeVolumeRange _VolumeRange; + private EEndpointHardwareSupport _HardwareSupport; + private AudioEndpointVolumeCallback _CallBack; + public event AudioEndpointVolumeNotificationDelegate OnVolumeNotification; + + public AudioEndPointVolumeVolumeRange VolumeRange + { + get + { + return _VolumeRange; + } + } + public EEndpointHardwareSupport HardwareSupport + { + get + { + return _HardwareSupport; + } + } + public AudioEndpointVolumeStepInformation StepInformation + { + get + { + return _StepInformation; + } + } + public AudioEndpointVolumeChannels Channels + { + get + { + return _Channels; + } + } + public float MasterVolumeLevel + { + get + { + float result; + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.GetMasterVolumeLevel(out result)); + return result; + } + set + { + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.SetMasterVolumeLevel(value, Guid.Empty)); + } + } + public float MasterVolumeLevelScalar + { + get + { + float result; + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.GetMasterVolumeLevelScalar(out result)); + return result; + } + set + { + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.SetMasterVolumeLevelScalar(value, Guid.Empty)); + } + } + public bool Mute + { + get + { + bool result; + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.GetMute(out result)); + return result; + } + set + { + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.SetMute(value, Guid.Empty)); + } + } + public void VolumeStepUp() + { + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.VolumeStepUp(Guid.Empty)); + } + public void VolumeStepDown() + { + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.VolumeStepDown(Guid.Empty)); + } + internal AudioEndpointVolume(IAudioEndpointVolume realEndpointVolume) + { + uint HardwareSupp; + + _AudioEndPointVolume = realEndpointVolume; + _Channels = new AudioEndpointVolumeChannels(_AudioEndPointVolume); + _StepInformation = new AudioEndpointVolumeStepInformation(_AudioEndPointVolume); + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.QueryHardwareSupport(out HardwareSupp)); + _HardwareSupport = (EEndpointHardwareSupport)HardwareSupp; + _VolumeRange = new AudioEndPointVolumeVolumeRange(_AudioEndPointVolume); + _CallBack = new AudioEndpointVolumeCallback(this); + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify( _CallBack)); + } + internal void FireNotification(AudioVolumeNotificationData NotificationData) + { + AudioEndpointVolumeNotificationDelegate del = OnVolumeNotification; + if (del != null) + { + del(NotificationData); + } + } + #region IDisposable Members + + public void Dispose() + { + if (_CallBack != null) + { + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.UnregisterControlChangeNotify( _CallBack )); + _CallBack = null; + } + } + + ~AudioEndpointVolume() + { + Dispose(); + } + + #endregion + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeCallback.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeCallback.cs new file mode 100644 index 0000000..84de641 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeCallback.cs @@ -0,0 +1,73 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System; +using System.Collections.Generic; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + // This class implements the IAudioEndpointVolumeCallback interface, + // it is implemented in this class because implementing it on AudioEndpointVolume + // (where the functionality is really wanted, would cause the OnNotify function + // to show up in the public API. + internal class AudioEndpointVolumeCallback : IAudioEndpointVolumeCallback + { + private AudioEndpointVolume _Parent; + + internal AudioEndpointVolumeCallback(AudioEndpointVolume parent) + { + _Parent = parent; + } + + [PreserveSig] public int OnNotify(IntPtr NotifyData) + { + //Since AUDIO_VOLUME_NOTIFICATION_DATA is dynamic in length based on the + //number of audio channels available we cannot just call PtrToStructure + //to get all data, thats why it is split up into two steps, first the static + //data is marshalled into the data structure, then with some IntPtr math the + //remaining floats are read from memory. + // + AUDIO_VOLUME_NOTIFICATION_DATA data = (AUDIO_VOLUME_NOTIFICATION_DATA) Marshal.PtrToStructure(NotifyData, typeof(AUDIO_VOLUME_NOTIFICATION_DATA)); + + //Determine offset in structure of the first float + IntPtr Offset = Marshal.OffsetOf(typeof(AUDIO_VOLUME_NOTIFICATION_DATA),"ChannelVolume"); + //Determine offset in memory of the first float + IntPtr FirstFloatPtr = (IntPtr)((long)NotifyData + (long)Offset); + + float[] voldata = new float[data.nChannels]; + + //Read all floats from memory. + for (int i = 0; i < data.nChannels; i++) + { + voldata[i] = (float)Marshal.PtrToStructure(FirstFloatPtr, typeof(float)); + } + + //Create combined structure and Fire Event in parent class. + AudioVolumeNotificationData NotificationData = new AudioVolumeNotificationData(data.guidEventContext, data.bMuted, data.fMasterVolume, voldata); + _Parent.FireNotification(NotificationData); + return 0; //S_OK + } + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeChannel.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeChannel.cs new file mode 100644 index 0000000..ccead51 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeChannel.cs @@ -0,0 +1,71 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System; +using System.Collections.Generic; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + public class AudioEndpointVolumeChannel + { + private uint _Channel; + private IAudioEndpointVolume _AudioEndpointVolume; + + internal AudioEndpointVolumeChannel(IAudioEndpointVolume parent, int channel) + { + _Channel = (uint)channel; + _AudioEndpointVolume = parent; + } + + public float VolumeLevel + { + get + { + float result; + Marshal.ThrowExceptionForHR(_AudioEndpointVolume.GetChannelVolumeLevel(_Channel,out result)); + return result; + } + set + { + Marshal.ThrowExceptionForHR(_AudioEndpointVolume.SetChannelVolumeLevel(_Channel, value,Guid.Empty)); + } + } + + public float VolumeLevelScalar + { + get + { + float result; + Marshal.ThrowExceptionForHR(_AudioEndpointVolume.GetChannelVolumeLevelScalar(_Channel, out result)); + return result; + } + set + { + Marshal.ThrowExceptionForHR(_AudioEndpointVolume.SetChannelVolumeLevelScalar(_Channel, value, Guid.Empty)); + } + } + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeChannels.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeChannels.cs new file mode 100644 index 0000000..5c6ce82 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeChannels.cs @@ -0,0 +1,68 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System; +using System.Collections.Generic; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + public class AudioEndpointVolumeChannels + { + IAudioEndpointVolume _AudioEndPointVolume; + AudioEndpointVolumeChannel[] _Channels; + public int Count + { + get + { + int result; + Marshal.ThrowExceptionForHR(_AudioEndPointVolume.GetChannelCount(out result)); + return result; + } + } + + public AudioEndpointVolumeChannel this[int index] + { + get + { + return _Channels[index]; + } + } + + internal AudioEndpointVolumeChannels(IAudioEndpointVolume parent) + { + int ChannelCount; + _AudioEndPointVolume = parent; + + ChannelCount = Count; + _Channels = new AudioEndpointVolumeChannel[ChannelCount]; + for (int i = 0; i < ChannelCount; i++) + { + _Channels[i] = new AudioEndpointVolumeChannel(_AudioEndPointVolume, i); + } + } + + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeNotificationDelegate.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeNotificationDelegate.cs new file mode 100644 index 0000000..4d97c4b --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeNotificationDelegate.cs @@ -0,0 +1,30 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System; +using System.Collections.Generic; +using System.Text; + +namespace CoreAudioApi +{ + public delegate void AudioEndpointVolumeNotificationDelegate( AudioVolumeNotificationData data); +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeStepInformation.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeStepInformation.cs new file mode 100644 index 0000000..bb77826 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioEndpointVolumeStepInformation.cs @@ -0,0 +1,56 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + public class AudioEndpointVolumeStepInformation + { + private uint _Step; + private uint _StepCount; + internal AudioEndpointVolumeStepInformation(IAudioEndpointVolume parent) + { + Marshal.ThrowExceptionForHR(parent.GetVolumeStepInfo(out _Step, out _StepCount)); + } + + public uint Step + { + get + { + return _Step; + } + } + + public uint StepCount + { + get + { + return _StepCount; + } + } + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioMeterInformation.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioMeterInformation.cs new file mode 100644 index 0000000..1294f9e --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioMeterInformation.cs @@ -0,0 +1,79 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + public class AudioMeterInformation + { + private IAudioMeterInformation _AudioMeterInformation; + private EEndpointHardwareSupport _HardwareSupport; + private AudioMeterInformationChannels _Channels; + + internal AudioMeterInformation(IAudioMeterInformation realInterface) + { + int HardwareSupp; + + _AudioMeterInformation = realInterface; + Marshal.ThrowExceptionForHR(_AudioMeterInformation.QueryHardwareSupport(out HardwareSupp)); + _HardwareSupport = (EEndpointHardwareSupport)HardwareSupp; + _Channels = new AudioMeterInformationChannels(_AudioMeterInformation); + + } + + public AudioMeterInformationChannels PeakValues + { + get + { + return _Channels; + } + } + + public EEndpointHardwareSupport HardwareSupport + { + get + { + return _HardwareSupport; + } + } + + public float MasterPeakValue + { + get + { + float result; + Marshal.ThrowExceptionForHR(_AudioMeterInformation.GetPeakValue(out result)); + return result; + } + } + + + + + + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioMeterInformationChannels.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioMeterInformationChannels.cs new file mode 100644 index 0000000..3f9f0c8 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioMeterInformationChannels.cs @@ -0,0 +1,61 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + public class AudioMeterInformationChannels + { + IAudioMeterInformation _AudioMeterInformation; + + public int Count + { + get + { + int result; + Marshal.ThrowExceptionForHR(_AudioMeterInformation.GetMeteringChannelCount(out result)); + return result; + } + } + + public float this[int index] + { + get + { + float[] peakValues = new float[Count]; + GCHandle Params = GCHandle.Alloc(peakValues, GCHandleType.Pinned); + Marshal.ThrowExceptionForHR(_AudioMeterInformation.GetChannelsPeakValues(peakValues.Length, Params.AddrOfPinnedObject())); + Params.Free(); + return peakValues[index]; + } + } + + internal AudioMeterInformationChannels(IAudioMeterInformation parent) + { + _AudioMeterInformation = parent; + } + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionControl.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionControl.cs new file mode 100644 index 0000000..51a7366 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionControl.cs @@ -0,0 +1,155 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + public class AudioSessionControl + { + internal IAudioSessionControl2 _AudioSessionControl; + internal AudioMeterInformation _AudioMeterInformation; + internal SimpleAudioVolume _SimpleAudioVolume; + + public AudioMeterInformation AudioMeterInformation + { + get + { + return _AudioMeterInformation; + } + } + + public SimpleAudioVolume SimpleAudioVolume + { + get + { + return _SimpleAudioVolume; + } + } + + + internal AudioSessionControl(IAudioSessionControl2 realAudioSessionControl) + { + IAudioMeterInformation _meters = realAudioSessionControl as IAudioMeterInformation; + ISimpleAudioVolume _volume = realAudioSessionControl as ISimpleAudioVolume; + if (_meters != null) + _AudioMeterInformation = new CoreAudioApi.AudioMeterInformation(_meters); + if (_volume != null) + _SimpleAudioVolume = new SimpleAudioVolume(_volume); + _AudioSessionControl = realAudioSessionControl; + + } + + public void RegisterAudioSessionNotification(IAudioSessionEvents eventConsumer) + { + Marshal.ThrowExceptionForHR(_AudioSessionControl.RegisterAudioSessionNotification(eventConsumer)); + } + + public void UnregisterAudioSessionNotification(IAudioSessionEvents eventConsumer) + { + Marshal.ThrowExceptionForHR(_AudioSessionControl.UnregisterAudioSessionNotification(eventConsumer)); + } + + public AudioSessionState State + { + get + { + AudioSessionState res; + Marshal.ThrowExceptionForHR(_AudioSessionControl.GetState(out res)); + return res; + } + } + + public string DisplayName + { + get + { + IntPtr NamePtr; + Marshal.ThrowExceptionForHR(_AudioSessionControl.GetDisplayName(out NamePtr)); + string res = Marshal.PtrToStringAuto(NamePtr); + Marshal.FreeCoTaskMem(NamePtr); + return res; + } + } + + public string IconPath + { + get + { + IntPtr NamePtr; + Marshal.ThrowExceptionForHR(_AudioSessionControl.GetIconPath(out NamePtr)); + string res = Marshal.PtrToStringAuto(NamePtr); + Marshal.FreeCoTaskMem(NamePtr); + return res; + } + } + + public string SessionIdentifier + { + get + { + IntPtr NamePtr; + Marshal.ThrowExceptionForHR(_AudioSessionControl.GetSessionIdentifier(out NamePtr)); + string res = Marshal.PtrToStringAuto(NamePtr); + Marshal.FreeCoTaskMem(NamePtr); + return res; + } + } + + public string SessionInstanceIdentifier + { + get + { + IntPtr NamePtr; + Marshal.ThrowExceptionForHR(_AudioSessionControl.GetSessionInstanceIdentifier(out NamePtr)); + string res = Marshal.PtrToStringAuto(NamePtr); + Marshal.FreeCoTaskMem(NamePtr); + return res; + } + } + + public uint ProcessID + { + get + { + uint pid; + Marshal.ThrowExceptionForHR(_AudioSessionControl.GetProcessId(out pid)); + return pid; + } + } + + public bool IsSystemIsSystemSoundsSession + { + get + { + return (_AudioSessionControl.IsSystemSoundsSession() == 0); //S_OK + } + + } + + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionDisconnectReason.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionDisconnectReason.cs new file mode 100644 index 0000000..3f4735f --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionDisconnectReason.cs @@ -0,0 +1,38 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CoreAudioApi +{ + public enum AudioSessionDisconnectReason + { + DisconnectReasonDeviceRemoval = 0, + DisconnectReasonServerShutdown = (DisconnectReasonDeviceRemoval + 1), + DisconnectReasonFormatChanged = (DisconnectReasonServerShutdown + 1), + DisconnectReasonSessionLogoff = (DisconnectReasonFormatChanged + 1), + DisconnectReasonSessionDisconnected = (DisconnectReasonSessionLogoff + 1), + DisconnectReasonExclusiveModeOverride = (DisconnectReasonSessionDisconnected + 1) + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionManager.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionManager.cs new file mode 100644 index 0000000..dccc1a3 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionManager.cs @@ -0,0 +1,54 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + public class AudioSessionManager + { + private IAudioSessionManager2 _AudioSessionManager; + private SessionCollection _Sessions; + + internal AudioSessionManager(IAudioSessionManager2 realAudioSessionManager) + { + _AudioSessionManager = realAudioSessionManager; + IAudioSessionEnumerator _SessionEnum ; + Marshal.ThrowExceptionForHR(_AudioSessionManager.GetSessionEnumerator(out _SessionEnum)); + _Sessions = new SessionCollection(_SessionEnum); + } + + public SessionCollection Sessions + { + get + { + return _Sessions; + } + } + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionState.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionState.cs new file mode 100644 index 0000000..ce85fcc --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioSessionState.cs @@ -0,0 +1,35 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace CoreAudioApi +{ + public enum AudioSessionState + { + AudioSessionStateInactive = 0, + AudioSessionStateActive = 1, + AudioSessionStateExpired = 2 + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/AudioVolumeNotificationData.cs b/AppleWirelessKeyboard/CoreAudioAPI/AudioVolumeNotificationData.cs new file mode 100644 index 0000000..cb18edf --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/AudioVolumeNotificationData.cs @@ -0,0 +1,83 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CoreAudioApi +{ + public class AudioVolumeNotificationData + { + private Guid _EventContext; + private bool _Muted; + private float _MasterVolume; + private int _Channels; + private float[] _ChannelVolume; + + public Guid EventContext + { + get + { + return _EventContext; + } + } + + public bool Muted + { + get + { + return _Muted; + } + } + + public float MasterVolume + { + get + { + return _MasterVolume; + } + } + public int Channels + { + get + { + return _Channels; + } + } + + public float[] ChannelVolume + { + get + { + return _ChannelVolume; + } + } + public AudioVolumeNotificationData(Guid eventContext, bool muted, float masterVolume, float[] channelVolume) + { + _EventContext = eventContext; + _Muted = muted; + _MasterVolume = masterVolume; + _Channels = channelVolume.Length; + _ChannelVolume = channelVolume; + } + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/Blob.cs b/AppleWirelessKeyboard/CoreAudioAPI/Blob.cs new file mode 100644 index 0000000..99ab275 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/Blob.cs @@ -0,0 +1,47 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + internal struct Blob + { + public int Length; + public IntPtr Data; + + //Code Should Compile at warning level4 without any warnings, + //However this struct will give us Warning CS0649: Field [Fieldname] + //is never assigned to, and will always have its default value + //You can disable CS0649 in the project options but that will disable + //the warning for the whole project, it's a nice warning and we do want + //it in other places so we make a nice dummy function to keep the compiler + //happy. + private void FixCS0649() + { + Length = 0; + Data = IntPtr.Zero; + } + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/CLSCTX.cs b/AppleWirelessKeyboard/CoreAudioAPI/CLSCTX.cs new file mode 100644 index 0000000..9a3352f --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/CLSCTX.cs @@ -0,0 +1,53 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CoreAudioApi.Interfaces +{ + [Flags] + internal enum CLSCTX : uint + { + INPROC_SERVER = 0x1, + INPROC_HANDLER = 0x2, + LOCAL_SERVER = 0x4, + INPROC_SERVER16 = 0x8, + REMOTE_SERVER = 0x10, + INPROC_HANDLER16 = 0x20, + RESERVED1 = 0x40, + RESERVED2 = 0x80, + RESERVED3 = 0x100, + RESERVED4 = 0x200, + NO_CODE_DOWNLOAD = 0x400, + RESERVED5 = 0x800, + NO_CUSTOM_MARSHAL = 0x1000, + ENABLE_CODE_DOWNLOAD = 0x2000, + NO_FAILURE_LOG = 0x4000, + DISABLE_AAA = 0x8000, + ENABLE_AAA = 0x10000, + FROM_DEFAULT_CONTEXT = 0x20000, + INPROC = INPROC_SERVER | INPROC_HANDLER, + SERVER = INPROC_SERVER | LOCAL_SERVER | REMOTE_SERVER, + ALL = SERVER | INPROC_HANDLER + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/EDataFlow.cs b/AppleWirelessKeyboard/CoreAudioAPI/EDataFlow.cs new file mode 100644 index 0000000..b43aa0d --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/EDataFlow.cs @@ -0,0 +1,35 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CoreAudioApi +{ + public enum EDataFlow + { + eRender = 0, + eCapture = 1, + eAll = 2 , + EDataFlow_enum_count =3 + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/EDeviceState.cs b/AppleWirelessKeyboard/CoreAudioAPI/EDeviceState.cs new file mode 100644 index 0000000..4b8b02e --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/EDeviceState.cs @@ -0,0 +1,36 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CoreAudioApi +{ + [Flags] + public enum EDeviceState : uint + { + DEVICE_STATE_ACTIVE = 0x00000001, + DEVICE_STATE_UNPLUGGED = 0x00000002, + DEVICE_STATE_NOTPRESENT = 0x00000004, + DEVICE_STATEMASK_ALL = 0x00000007 + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/EEndpointHardwareSupport.cs b/AppleWirelessKeyboard/CoreAudioAPI/EEndpointHardwareSupport.cs new file mode 100644 index 0000000..a394b43 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/EEndpointHardwareSupport.cs @@ -0,0 +1,35 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CoreAudioApi +{ + [Flags] + public enum EEndpointHardwareSupport + { + Volume = 0x00000001, + Mute = 0x00000002, + Meter = 0x00000004 + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/ERole.cs b/AppleWirelessKeyboard/CoreAudioAPI/ERole.cs new file mode 100644 index 0000000..b19973b --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/ERole.cs @@ -0,0 +1,35 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CoreAudioApi +{ + public enum ERole + { + eConsole =0, + eMultimedia =1, + eCommunications=2, + ERole_enum_count=3 + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IAudioEndpointVolume.cs b/AppleWirelessKeyboard/CoreAudioAPI/IAudioEndpointVolume.cs new file mode 100644 index 0000000..2af5c13 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IAudioEndpointVolume.cs @@ -0,0 +1,70 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + [Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + internal interface IAudioEndpointVolume + { + [PreserveSig] + int RegisterControlChangeNotify(IAudioEndpointVolumeCallback pNotify); + [PreserveSig] + int UnregisterControlChangeNotify(IAudioEndpointVolumeCallback pNotify); + [PreserveSig] + int GetChannelCount(out int pnChannelCount); + [PreserveSig] + int SetMasterVolumeLevel(float fLevelDB, Guid pguidEventContext); + [PreserveSig] + int SetMasterVolumeLevelScalar(float fLevel, Guid pguidEventContext); + [PreserveSig] + int GetMasterVolumeLevel(out float pfLevelDB); + [PreserveSig] + int GetMasterVolumeLevelScalar(out float pfLevel); + [PreserveSig] + int SetChannelVolumeLevel(uint nChannel, float fLevelDB, Guid pguidEventContext); + [PreserveSig] + int SetChannelVolumeLevelScalar(uint nChannel, float fLevel, Guid pguidEventContext); + [PreserveSig] + int GetChannelVolumeLevel(uint nChannel, out float pfLevelDB); + [PreserveSig] + int GetChannelVolumeLevelScalar(uint nChannel, out float pfLevel); + [PreserveSig] + int SetMute([MarshalAs(UnmanagedType.Bool)] Boolean bMute, Guid pguidEventContext); + [PreserveSig] + int GetMute(out bool pbMute); + [PreserveSig] + int GetVolumeStepInfo(out uint pnStep, out uint pnStepCount); + [PreserveSig] + int VolumeStepUp(Guid pguidEventContext); + [PreserveSig] + int VolumeStepDown(Guid pguidEventContext); + [PreserveSig] + int QueryHardwareSupport(out uint pdwHardwareSupportMask); + [PreserveSig] + int GetVolumeRange(out float pflVolumeMindB, out float pflVolumeMaxdB, out float pflVolumeIncrementdB); + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IAudioEndpointVolumeCallback.cs b/AppleWirelessKeyboard/CoreAudioAPI/IAudioEndpointVolumeCallback.cs new file mode 100644 index 0000000..50e8fe9 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IAudioEndpointVolumeCallback.cs @@ -0,0 +1,36 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + [Guid("657804FA-D6AD-4496-8A60-352752AF4F89"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + internal interface IAudioEndpointVolumeCallback + { + [PreserveSig] int OnNotify(IntPtr pNotifyData); + }; + +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IAudioMeterInformation.cs b/AppleWirelessKeyboard/CoreAudioAPI/IAudioMeterInformation.cs new file mode 100644 index 0000000..37a7ae3 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IAudioMeterInformation.cs @@ -0,0 +1,42 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + [Guid("C02216F6-8C67-4B5B-9D00-D008E73E0064"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + internal interface IAudioMeterInformation + { + [PreserveSig] + int GetPeakValue(out float pfPeak); + [PreserveSig] + int GetMeteringChannelCount(out int pnChannelCount); + [PreserveSig] + int GetChannelsPeakValues( int u32ChannelCount,[In] IntPtr afPeakValues); + [PreserveSig] + int QueryHardwareSupport( out int pdwHardwareSupportMask); + }; +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IAudioPeakMeter.cs b/AppleWirelessKeyboard/CoreAudioAPI/IAudioPeakMeter.cs new file mode 100644 index 0000000..c2d402d --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IAudioPeakMeter.cs @@ -0,0 +1,37 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + + +namespace CoreAudioApi.Interfaces +{ + [Guid("DD79923C-0599-45e0-B8B6-C8DF7DB6E796"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + internal interface IAudioPeakMeter + { + int GetChannelCount( out int pcChannels); + int GetLevel( int Channel, out float level); + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionControl2.cs b/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionControl2.cs new file mode 100644 index 0000000..35ad8c3 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionControl2.cs @@ -0,0 +1,66 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + [Guid("bfb7ff88-7239-4fc9-8fa2-07c950be9c6d"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + interface IAudioSessionControl2 + { + //IAudioSession functions + [PreserveSig] + int GetState(out AudioSessionState state); + [PreserveSig] + int GetDisplayName(out IntPtr name); + [PreserveSig] + int SetDisplayName(string value, Guid EventContext); + [PreserveSig] + int GetIconPath(out IntPtr Path); + [PreserveSig] + int SetIconPath(string Value, Guid EventContext); + [PreserveSig] + int GetGroupingParam(out Guid GroupingParam); + [PreserveSig] + int SetGroupingParam(Guid Override, Guid Eventcontext); + [PreserveSig] + int RegisterAudioSessionNotification(IAudioSessionEvents NewNotifications); + [PreserveSig] + int UnregisterAudioSessionNotification(IAudioSessionEvents NewNotifications); + //IAudioSession2 functions + [PreserveSig] + int GetSessionIdentifier( out IntPtr retVal); + [PreserveSig] + int GetSessionInstanceIdentifier( out IntPtr retVal); + [PreserveSig] + int GetProcessId( out UInt32 retvVal); + [PreserveSig] + int IsSystemSoundsSession(); + [PreserveSig] + int SetDuckingPreference( bool optOut); + + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionEnumerator.cs b/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionEnumerator.cs new file mode 100644 index 0000000..9ab89fc --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionEnumerator.cs @@ -0,0 +1,34 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System.Runtime.InteropServices; +using System; +namespace CoreAudioApi.Interfaces +{ + [Guid("E2F5BB11-0570-40CA-ACDD-3AA01277DEE8"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + internal interface IAudioSessionEnumerator + { + int GetCount( out int SessionCount); + int GetSession( int SessionCount,out IAudioSessionControl2 Session ); + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionEvents.cs b/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionEvents.cs new file mode 100644 index 0000000..a3335c0 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionEvents.cs @@ -0,0 +1,48 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + [Guid("24918ACC-64B3-37C1-8CA9-74A66E9957A8"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + public interface IAudioSessionEvents + { + [PreserveSig] + int OnDisplayNameChanged( [MarshalAs(UnmanagedType.LPWStr)] string NewDisplayName, Guid EventContext ); + [PreserveSig] + int OnIconPathChanged( [MarshalAs(UnmanagedType.LPWStr)] string NewIconPath, Guid EventContext ); + [PreserveSig] + int OnSimpleVolumeChanged( float NewVolume,bool newMute, Guid EventContext ); + [PreserveSig] + int OnChannelVolumeChanged( UInt32 ChannelCount, IntPtr NewChannelVolumeArray, UInt32 ChangedChannel, Guid EventContext ); + [PreserveSig] + int OnGroupingParamChanged( Guid NewGroupingParam, Guid EventContext ); + [PreserveSig] + int OnStateChanged( AudioSessionState NewState); + [PreserveSig] + int OnSessionDisconnected( AudioSessionDisconnectReason DisconnectReason); + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionManager2.cs b/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionManager2.cs new file mode 100644 index 0000000..eb0d809 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IAudioSessionManager2.cs @@ -0,0 +1,49 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + [Guid("77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + interface IAudioSessionManager2 + { + [PreserveSig] + int GetAudioSessionControl(ref Guid AudioSessionGuid, UInt32 StreamFlags, IntPtr ISessionControl ); + [PreserveSig] + int GetSimpleAudioVolume(ref Guid AudioSessionGuid, UInt32 StreamFlags, IntPtr /*ISimpleAudioVolume*/ SimpleAudioVolume); + [PreserveSig] + int GetSessionEnumerator(out IAudioSessionEnumerator SessionEnum); + [PreserveSig] + int RegisterSessionNotification( IntPtr IAudioSessionNotification ); + [PreserveSig] + int UnregisterSessionNotification( IntPtr IAudioSessionNotification ); + [PreserveSig] + int RegisterDuckNotification( string sessionID, IntPtr IAudioVolumeDuckNotification); + [PreserveSig] + int UnregisterDuckNotification(IntPtr IAudioVolumeDuckNotification); + }; +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IMMDevice.cs b/AppleWirelessKeyboard/CoreAudioAPI/IMMDevice.cs new file mode 100644 index 0000000..c79ac4b --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IMMDevice.cs @@ -0,0 +1,42 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + [Guid("D666063F-1587-4E43-81F1-B948E807363F"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + internal interface IMMDevice + { + [PreserveSig] + int Activate(ref Guid iid, CLSCTX dwClsCtx, IntPtr pActivationParams, [MarshalAs(UnmanagedType.IUnknown)] out object ppInterface); + [PreserveSig] + int OpenPropertyStore(EStgmAccess stgmAccess, out IPropertyStore propertyStore); + [PreserveSig] + int GetId([MarshalAs(UnmanagedType.LPWStr)] out string ppstrId); + [PreserveSig] + int GetState(out EDeviceState pdwState); + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IMMDeviceCollection.cs b/AppleWirelessKeyboard/CoreAudioAPI/IMMDeviceCollection.cs new file mode 100644 index 0000000..eb0e58f --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IMMDeviceCollection.cs @@ -0,0 +1,38 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + [Guid("0BD7A1BE-7A1A-44DB-8397-CC5392387B5E"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + internal interface IMMDeviceCollection + { + [PreserveSig] + int GetCount(out uint pcDevices); + [PreserveSig] + int Item(uint nDevice, out IMMDevice Device); + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IMMDeviceEnumerator.cs b/AppleWirelessKeyboard/CoreAudioAPI/IMMDeviceEnumerator.cs new file mode 100644 index 0000000..68b1f8c --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IMMDeviceEnumerator.cs @@ -0,0 +1,44 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + internal interface IMMDeviceEnumerator + { + [PreserveSig] + int EnumAudioEndpoints(EDataFlow dataFlow, EDeviceState StateMask, out IMMDeviceCollection device); + [PreserveSig] + int GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role, out IMMDevice ppEndpoint); + [PreserveSig] + int GetDevice(string pwstrId, out IMMDevice ppDevice); + [PreserveSig] + int RegisterEndpointNotificationCallback(IntPtr pClient); + [PreserveSig] + int UnregisterEndpointNotificationCallback(IntPtr pClient); + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IMMEndpoint.cs b/AppleWirelessKeyboard/CoreAudioAPI/IMMEndpoint.cs new file mode 100644 index 0000000..ccc5de4 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IMMEndpoint.cs @@ -0,0 +1,36 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + [Guid("1BE09788-6894-4089-8586-9A2A6C265AC5"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + internal interface IMMEndpoint + { + [PreserveSig] + int GetDataFlow(out EDataFlow pDataFlow); + }; +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/IPropertyStore.cs b/AppleWirelessKeyboard/CoreAudioAPI/IPropertyStore.cs new file mode 100644 index 0000000..70b22c8 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/IPropertyStore.cs @@ -0,0 +1,44 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + +namespace CoreAudioApi.Interfaces +{ + [Guid("886d8eeb-8cf2-4446-8d02-cdba1dbdcf99"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + internal interface IPropertyStore + { + [PreserveSig] + int GetCount( out Int32 count); + [PreserveSig] + int GetAt( int iProp, out PropertyKey pkey); + [PreserveSig] + int GetValue(ref PropertyKey key, out PropVariant pv); + [PreserveSig] + int SetValue(ref PropertyKey key, ref PropVariant propvar); + [PreserveSig] + int Commit( ); + }; +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/ISimpleAudioVolume.cs b/AppleWirelessKeyboard/CoreAudioAPI/ISimpleAudioVolume.cs new file mode 100644 index 0000000..8d47d86 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/ISimpleAudioVolume.cs @@ -0,0 +1,43 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; + + +namespace CoreAudioApi.Interfaces +{ + [Guid("87CE5498-68D6-44E5-9215-6DA47EF883D8"), + InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + internal interface ISimpleAudioVolume + { + [PreserveSig] + int SetMasterVolume( float fLevel,ref Guid EventContext); + [PreserveSig] + int GetMasterVolume( out float pfLevel); + [PreserveSig] + int SetMute( bool bMute, ref Guid EventContext) ; + [PreserveSig] + int GetMute( out bool bMute); + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/MMDevice.cs b/AppleWirelessKeyboard/CoreAudioAPI/MMDevice.cs new file mode 100644 index 0000000..7563750 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/MMDevice.cs @@ -0,0 +1,180 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + public class MMDevice + { + #region Variables + private IMMDevice _RealDevice; + private PropertyStore _PropertyStore; + private AudioMeterInformation _AudioMeterInformation; + private AudioEndpointVolume _AudioEndpointVolume; + private AudioSessionManager _AudioSessionManager; + + #endregion + + #region Guids + private static Guid IID_IAudioMeterInformation = typeof(IAudioMeterInformation).GUID; + private static Guid IID_IAudioEndpointVolume = typeof(IAudioEndpointVolume).GUID; + private static Guid IID_IAudioSessionManager = typeof(IAudioSessionManager2).GUID; + #endregion + + #region Init + private void GetPropertyInformation() + { + IPropertyStore propstore; + Marshal.ThrowExceptionForHR(_RealDevice.OpenPropertyStore(EStgmAccess.STGM_READ, out propstore)); + _PropertyStore = new PropertyStore(propstore); + } + + private void GetAudioSessionManager() + { + object result; + Marshal.ThrowExceptionForHR(_RealDevice.Activate(ref IID_IAudioSessionManager, CLSCTX.ALL, IntPtr.Zero, out result)); + _AudioSessionManager = new AudioSessionManager(result as IAudioSessionManager2); + } + + private void GetAudioMeterInformation() + { + object result; + Marshal.ThrowExceptionForHR( _RealDevice.Activate(ref IID_IAudioMeterInformation, CLSCTX.ALL, IntPtr.Zero, out result)); + _AudioMeterInformation = new AudioMeterInformation( result as IAudioMeterInformation); + } + + private void GetAudioEndpointVolume() + { + object result; + Marshal.ThrowExceptionForHR(_RealDevice.Activate(ref IID_IAudioEndpointVolume, CLSCTX.ALL, IntPtr.Zero, out result)); + _AudioEndpointVolume = new AudioEndpointVolume(result as IAudioEndpointVolume); + } + + #endregion + + #region Properties + + public AudioSessionManager AudioSessionManager + { + get + { + if (_AudioSessionManager == null) + GetAudioSessionManager(); + + return _AudioSessionManager; + } + } + + public AudioMeterInformation AudioMeterInformation + { + get + { + if (_AudioMeterInformation == null) + GetAudioMeterInformation(); + + return _AudioMeterInformation; + } + } + + public AudioEndpointVolume AudioEndpointVolume + { + get + { + if (_AudioEndpointVolume == null) + GetAudioEndpointVolume(); + + return _AudioEndpointVolume; + } + } + + public PropertyStore Properties + { + get + { + if (_PropertyStore == null) + GetPropertyInformation(); + return _PropertyStore; + } + } + + public string FriendlyName + { + get + { + if (_PropertyStore == null) + GetPropertyInformation(); + if (_PropertyStore.Contains(PKEY.PKEY_DeviceInterface_FriendlyName)) + { + return (string)_PropertyStore[PKEY.PKEY_DeviceInterface_FriendlyName].Value; + } + else + return "Unknown"; + } + } + + + public string ID + { + get + { + string Result; + Marshal.ThrowExceptionForHR(_RealDevice.GetId(out Result)); + return Result; + } + } + + public EDataFlow DataFlow + { + get + { + EDataFlow Result; + IMMEndpoint ep = _RealDevice as IMMEndpoint ; + ep.GetDataFlow(out Result); + return Result; + } + } + + public EDeviceState State + { + get + { + EDeviceState Result; + Marshal.ThrowExceptionForHR(_RealDevice.GetState(out Result)); + return Result; + + } + } + #endregion + + #region Constructor + internal MMDevice(IMMDevice realDevice) + { + _RealDevice = realDevice; + } + #endregion + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/MMDeviceCollection.cs b/AppleWirelessKeyboard/CoreAudioAPI/MMDeviceCollection.cs new file mode 100644 index 0000000..a2b96d2 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/MMDeviceCollection.cs @@ -0,0 +1,59 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; +using CoreAudioApi.Interfaces; + +namespace CoreAudioApi +{ + public class MMDeviceCollection + { + private IMMDeviceCollection _MMDeviceCollection; + + public int Count + { + get + { + uint result; + Marshal.ThrowExceptionForHR(_MMDeviceCollection.GetCount(out result)); + return (int)result; + } + } + + public MMDevice this[int index] + { + get + { + IMMDevice result; + _MMDeviceCollection.Item((uint)index, out result); + return new MMDevice(result); + } + } + + internal MMDeviceCollection(IMMDeviceCollection parent) + { + _MMDeviceCollection = parent; + } + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/MMDeviceEnumerator.cs b/AppleWirelessKeyboard/CoreAudioAPI/MMDeviceEnumerator.cs new file mode 100644 index 0000000..91ba5d6 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/MMDeviceEnumerator.cs @@ -0,0 +1,70 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; +using CoreAudioApi.Interfaces; + +namespace CoreAudioApi +{ + //Marked as internal, since on its own its no good + [ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] + internal class _MMDeviceEnumerator + { + } + + //Small wrapper class + public class MMDeviceEnumerator + { + private IMMDeviceEnumerator _realEnumerator = new _MMDeviceEnumerator() as IMMDeviceEnumerator; + + public MMDeviceCollection EnumerateAudioEndPoints(EDataFlow dataFlow, EDeviceState dwStateMask) + { + IMMDeviceCollection result; + Marshal.ThrowExceptionForHR(_realEnumerator.EnumAudioEndpoints(dataFlow,dwStateMask,out result)); + return new MMDeviceCollection(result); + } + + public MMDevice GetDefaultAudioEndpoint(EDataFlow dataFlow, ERole role) + { + IMMDevice _Device = null; + Marshal.ThrowExceptionForHR(((IMMDeviceEnumerator)_realEnumerator).GetDefaultAudioEndpoint(dataFlow, role, out _Device)); + return new MMDevice(_Device); + } + + public MMDevice GetDevice(string ID) + { + IMMDevice _Device = null; + Marshal.ThrowExceptionForHR(((IMMDeviceEnumerator)_realEnumerator).GetDevice(ID, out _Device)); + return new MMDevice(_Device); + } + + public MMDeviceEnumerator() + { + if (System.Environment.OSVersion.Version.Major < 6) + { + throw new NotSupportedException("This functionality is only supported on Windows Vista or newer."); + } + } + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/PKEY.cs b/AppleWirelessKeyboard/CoreAudioAPI/PKEY.cs new file mode 100644 index 0000000..fda79b5 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/PKEY.cs @@ -0,0 +1,41 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CoreAudioApi +{ + public static class PKEY + { + public static readonly Guid PKEY_DeviceInterface_FriendlyName = new Guid( 0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0); + public static readonly Guid PKEY_AudioEndpoint_FormFactor = new Guid( 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x0e); + public static readonly Guid PKEY_AudioEndpoint_ControlPanelPageProvider = new Guid( 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x0e); + public static readonly Guid PKEY_AudioEndpoint_Association = new Guid( 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x0e); + public static readonly Guid PKEY_AudioEndpoint_PhysicalSpeakers = new Guid( 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x0e); + public static readonly Guid PKEY_AudioEndpoint_GUID = new Guid( 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x0e); + public static readonly Guid PKEY_AudioEndpoint_Disable_SysFx = new Guid( 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x0e); + public static readonly Guid PKEY_AudioEndpoint_FullRangeSpeakers = new Guid( 0x1da5d803, 0xd492, 0x4edd, 0x8c, 0x23, 0xe0, 0xc0, 0xff, 0xee, 0x7f, 0x0e); + public static readonly Guid PKEY_AudioEngine_DeviceFormat = new Guid(0xf19f064d, 0x82c, 0x4e27, 0xbc, 0x73, 0x68, 0x82, 0xa1, 0xbb, 0x8e, 0x4c); + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/PropVariant.cs b/AppleWirelessKeyboard/CoreAudioAPI/PropVariant.cs new file mode 100644 index 0000000..92e5367 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/PropVariant.cs @@ -0,0 +1,96 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices.ComTypes; +using System.Runtime.InteropServices; +using CoreAudioApi.Interfaces; + +namespace CoreAudioApi +{ + [StructLayout(LayoutKind.Explicit)] + public struct PropVariant + { + [FieldOffset(0)] short vt; + [FieldOffset(2)] short wReserved1; + [FieldOffset(4)] short wReserved2; + [FieldOffset(6)] short wReserved3; + [FieldOffset(8)] sbyte cVal; + [FieldOffset(8)] byte bVal; + [FieldOffset(8)] short iVal; + [FieldOffset(8)] ushort uiVal; + [FieldOffset(8)] int lVal; + [FieldOffset(8)] uint ulVal; + [FieldOffset(8)] long hVal; + [FieldOffset(8)] ulong uhVal; + [FieldOffset(8)] float fltVal; + [FieldOffset(8)] double dblVal; + [FieldOffset(8)] Blob blobVal; + [FieldOffset(8)] DateTime date; + [FieldOffset(8)] bool boolVal; + [FieldOffset(8)] int scode; + [FieldOffset(8)] System.Runtime.InteropServices.ComTypes.FILETIME filetime; + [FieldOffset(8)] IntPtr everything_else; + + //I'm sure there is a more efficient way to do this but this works ..for now.. + internal byte[] GetBlob() + { + byte[] Result = new byte[blobVal.Length]; + for (int i = 0; i < blobVal.Length; i++) + { + Result[i] = Marshal.ReadByte((IntPtr) ((long)(blobVal.Data) + i)); + } + return Result; + } + + public object Value + { + get + { + VarEnum ve = (VarEnum)vt; + switch (ve) + { + case VarEnum.VT_I1: + return bVal; + case VarEnum.VT_I2: + return iVal; + case VarEnum.VT_I4: + return lVal; + case VarEnum.VT_I8: + return hVal; + case VarEnum.VT_INT: + return iVal; + case VarEnum.VT_UI4: + return ulVal; + case VarEnum.VT_LPWSTR: + return Marshal.PtrToStringUni(everything_else); + case VarEnum.VT_BLOB: + return GetBlob(); + } + return "FIXME Type = " + ve.ToString(); + } + } + + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/PropertyKey.cs b/AppleWirelessKeyboard/CoreAudioAPI/PropertyKey.cs new file mode 100644 index 0000000..14dadc3 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/PropertyKey.cs @@ -0,0 +1,34 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CoreAudioApi +{ + public struct PropertyKey + { + public Guid fmtid; + public int pid; + }; + +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/PropertyStore.cs b/AppleWirelessKeyboard/CoreAudioAPI/PropertyStore.cs new file mode 100644 index 0000000..3651e39 --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/PropertyStore.cs @@ -0,0 +1,107 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + /// + /// Property Store class, only supports reading properties at the moment. + /// + public class PropertyStore + { + private IPropertyStore _Store; + + public int Count + { + get + { + int Result; + Marshal.ThrowExceptionForHR(_Store.GetCount(out Result)); + return Result; + } + } + + public PropertyStoreProperty this[int index] + { + get + { + PropVariant result; + PropertyKey key = Get(index); + Marshal.ThrowExceptionForHR(_Store.GetValue(ref key, out result)); + return new PropertyStoreProperty(key, result); + } + } + + public bool Contains(Guid guid) + { + for (int i = 0; i < Count; i++) + { + PropertyKey key = Get(i); + if (key.fmtid == guid) + return true; + } + return false; + } + + public PropertyStoreProperty this[Guid guid] + { + get + { + PropVariant result; + for (int i = 0; i < Count; i++) + { + PropertyKey key = Get(i); + if (key.fmtid == guid) + { + Marshal.ThrowExceptionForHR(_Store.GetValue(ref key, out result)); + return new PropertyStoreProperty(key, result); + } + } + return null; + } + } + + public PropertyKey Get(int index) + { + PropertyKey key; + Marshal.ThrowExceptionForHR( _Store.GetAt(index, out key)); + return key; + } + + public PropVariant GetValue(int index) + { + PropVariant result; + PropertyKey key = Get(index); + Marshal.ThrowExceptionForHR(_Store.GetValue(ref key, out result)); + return result; + } + + internal PropertyStore(IPropertyStore store) + { + _Store = store; + } + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/PropertyStoreProperty.cs b/AppleWirelessKeyboard/CoreAudioAPI/PropertyStoreProperty.cs new file mode 100644 index 0000000..2090f0a --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/PropertyStoreProperty.cs @@ -0,0 +1,57 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System; +using System.Collections.Generic; +using System.Text; +using CoreAudioApi.Interfaces; + +namespace CoreAudioApi +{ + public class PropertyStoreProperty + { + private PropertyKey _PropertyKey; + private PropVariant _PropValue; + + internal PropertyStoreProperty(PropertyKey key, PropVariant value) + { + _PropertyKey = key; + _PropValue = value; + } + + public PropertyKey Key + { + get + { + return _PropertyKey; + } + } + + public object Value + { + get + { + return _PropValue.Value; + } + } + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/SessionCollection.cs b/AppleWirelessKeyboard/CoreAudioAPI/SessionCollection.cs new file mode 100644 index 0000000..1260e5e --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/SessionCollection.cs @@ -0,0 +1,60 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + public class SessionCollection + { + IAudioSessionEnumerator _AudioSessionEnumerator; + internal SessionCollection(IAudioSessionEnumerator realEnumerator) + { + _AudioSessionEnumerator = realEnumerator; + } + + public AudioSessionControl this[int index] + { + get + { + IAudioSessionControl2 _Result; + Marshal.ThrowExceptionForHR(_AudioSessionEnumerator.GetSession(index, out _Result)); + return new AudioSessionControl(_Result); + } + } + + public int Count + { + get + { + int result; + Marshal.ThrowExceptionForHR(_AudioSessionEnumerator.GetCount(out result)); + return (int)result; + } + } + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/SimpleAudioVolume.cs b/AppleWirelessKeyboard/CoreAudioAPI/SimpleAudioVolume.cs new file mode 100644 index 0000000..c46bacd --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/SimpleAudioVolume.cs @@ -0,0 +1,70 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using CoreAudioApi.Interfaces; +using System.Runtime.InteropServices; + +namespace CoreAudioApi +{ + public class SimpleAudioVolume + { + ISimpleAudioVolume _SimpleAudioVolume; + internal SimpleAudioVolume(ISimpleAudioVolume realSimpleVolume) + { + _SimpleAudioVolume = realSimpleVolume; + } + + public float MasterVolume + { + get + { + float ret; + Marshal.ThrowExceptionForHR(_SimpleAudioVolume.GetMasterVolume(out ret)); + return ret; + } + set + { + Guid Empty = Guid.Empty; + Marshal.ThrowExceptionForHR(_SimpleAudioVolume.SetMasterVolume(value, ref Empty)); + } + } + + public bool Mute + { + get + { + bool ret; + Marshal.ThrowExceptionForHR(_SimpleAudioVolume.GetMute(out ret)); + return ret; + } + set + { + Guid Empty = Guid.Empty; + Marshal.ThrowExceptionForHR(_SimpleAudioVolume.SetMute(value, ref Empty)); + } + } + } +} diff --git a/AppleWirelessKeyboard/CoreAudioAPI/eStgmAccess.cs b/AppleWirelessKeyboard/CoreAudioAPI/eStgmAccess.cs new file mode 100644 index 0000000..17dbb8b --- /dev/null +++ b/AppleWirelessKeyboard/CoreAudioAPI/eStgmAccess.cs @@ -0,0 +1,34 @@ +/* + LICENSE + ------- + Copyright (C) 2007-2010 Ray Molenkamp + + This source code is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this source code or the software it produces. + + Permission is granted to anyone to use this source code for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this source code must not be misrepresented; you must not + claim that you wrote the original source code. If you use this source code + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original source code. + 3. This notice may not be removed or altered from any source distribution. +*/ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CoreAudioApi.Interfaces +{ + internal enum EStgmAccess + { + STGM_READ = 0x00000000, + STGM_WRITE = 0x00000001, + STGM_READWRITE = 0x00000002 + } +} diff --git a/AppleWirelessKeyboard/Glyphs/Eject.xaml b/AppleWirelessKeyboard/Glyphs/Eject.xaml new file mode 100644 index 0000000..97f87c2 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/Eject.xaml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/Eject.xaml.cs b/AppleWirelessKeyboard/Glyphs/Eject.xaml.cs new file mode 100644 index 0000000..25c3ce8 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/Eject.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for Eject.xaml + /// + public partial class Eject : UserControl + { + public Eject() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Glyphs/MuteOff.xaml b/AppleWirelessKeyboard/Glyphs/MuteOff.xaml new file mode 100644 index 0000000..4a036fb --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/MuteOff.xaml @@ -0,0 +1,13 @@ + + + + + + + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/MuteOff.xaml.cs b/AppleWirelessKeyboard/Glyphs/MuteOff.xaml.cs new file mode 100644 index 0000000..18fb478 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/MuteOff.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for MuteOff.xaml + /// + public partial class MuteOff : UserControl + { + public MuteOff() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Glyphs/MuteOn.xaml b/AppleWirelessKeyboard/Glyphs/MuteOn.xaml new file mode 100644 index 0000000..58feb64 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/MuteOn.xaml @@ -0,0 +1,11 @@ + + + + + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/MuteOn.xaml.cs b/AppleWirelessKeyboard/Glyphs/MuteOn.xaml.cs new file mode 100644 index 0000000..8f7b28a --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/MuteOn.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for MuteOn.xaml + /// + public partial class MuteOn : UserControl + { + public MuteOn() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Glyphs/Next.xaml b/AppleWirelessKeyboard/Glyphs/Next.xaml new file mode 100644 index 0000000..9e12423 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/Next.xaml @@ -0,0 +1,8 @@ + + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/Next.xaml.cs b/AppleWirelessKeyboard/Glyphs/Next.xaml.cs new file mode 100644 index 0000000..0e65c68 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/Next.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for Next.xaml + /// + public partial class Next : UserControl + { + public Next() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Glyphs/Off.xaml b/AppleWirelessKeyboard/Glyphs/Off.xaml new file mode 100644 index 0000000..72df273 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/Off.xaml @@ -0,0 +1,7 @@ + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/Off.xaml.cs b/AppleWirelessKeyboard/Glyphs/Off.xaml.cs new file mode 100644 index 0000000..b3ca1a3 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/Off.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for Off.xaml + /// + public partial class Off : UserControl + { + public Off() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Glyphs/On.xaml b/AppleWirelessKeyboard/Glyphs/On.xaml new file mode 100644 index 0000000..67c41ee --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/On.xaml @@ -0,0 +1,7 @@ + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/On.xaml.cs b/AppleWirelessKeyboard/Glyphs/On.xaml.cs new file mode 100644 index 0000000..cdca88d --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/On.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for On.xaml + /// + public partial class On : UserControl + { + public On() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Glyphs/PlayPause.xaml b/AppleWirelessKeyboard/Glyphs/PlayPause.xaml new file mode 100644 index 0000000..10fbe12 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/PlayPause.xaml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/PlayPause.xaml.cs b/AppleWirelessKeyboard/Glyphs/PlayPause.xaml.cs new file mode 100644 index 0000000..3b90f98 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/PlayPause.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for Play.xaml + /// + public partial class PlayPause : UserControl + { + public PlayPause() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Glyphs/Previous.xaml b/AppleWirelessKeyboard/Glyphs/Previous.xaml new file mode 100644 index 0000000..8095ba6 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/Previous.xaml @@ -0,0 +1,8 @@ + + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/Previous.xaml.cs b/AppleWirelessKeyboard/Glyphs/Previous.xaml.cs new file mode 100644 index 0000000..e6b3804 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/Previous.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for Previous.xaml + /// + public partial class Previous : UserControl + { + public Previous() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Glyphs/PrintScreen.xaml b/AppleWirelessKeyboard/Glyphs/PrintScreen.xaml new file mode 100644 index 0000000..3e52ffd --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/PrintScreen.xaml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/PrintScreen.xaml.cs b/AppleWirelessKeyboard/Glyphs/PrintScreen.xaml.cs new file mode 100644 index 0000000..e84c6aa --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/PrintScreen.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for PrintScreen.xaml + /// + public partial class PrintScreen : UserControl + { + public PrintScreen() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Glyphs/TaskManager.xaml b/AppleWirelessKeyboard/Glyphs/TaskManager.xaml new file mode 100644 index 0000000..00cf0f8 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/TaskManager.xaml @@ -0,0 +1,10 @@ + + + + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/TaskManager.xaml.cs b/AppleWirelessKeyboard/Glyphs/TaskManager.xaml.cs new file mode 100644 index 0000000..03ac6b9 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/TaskManager.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for TaskManager.xaml + /// + public partial class TaskManager : UserControl + { + public TaskManager() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Glyphs/VolumeOff.xaml b/AppleWirelessKeyboard/Glyphs/VolumeOff.xaml new file mode 100644 index 0000000..de7613f --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/VolumeOff.xaml @@ -0,0 +1,14 @@ + + + + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/VolumeOff.xaml.cs b/AppleWirelessKeyboard/Glyphs/VolumeOff.xaml.cs new file mode 100644 index 0000000..1d6b117 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/VolumeOff.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for VolumeOff.xaml + /// + public partial class VolumeOff : UserControl + { + public VolumeOff() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Glyphs/VolumeOn.xaml b/AppleWirelessKeyboard/Glyphs/VolumeOn.xaml new file mode 100644 index 0000000..272a999 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/VolumeOn.xaml @@ -0,0 +1,17 @@ + + + + + + + + + + + diff --git a/AppleWirelessKeyboard/Glyphs/VolumeOn.xaml.cs b/AppleWirelessKeyboard/Glyphs/VolumeOn.xaml.cs new file mode 100644 index 0000000..a4e3119 --- /dev/null +++ b/AppleWirelessKeyboard/Glyphs/VolumeOn.xaml.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace AppleWirelessKeyboard.Glyphs +{ + /// + /// Interaction logic for VolumeOn.xaml + /// + public partial class VolumeOn : UserControl + { + public VolumeOn() + { + InitializeComponent(); + } + } +} diff --git a/AppleWirelessKeyboard/Gnome-Preferences-Desktop-Keyboard-Shortcuts.ico b/AppleWirelessKeyboard/Gnome-Preferences-Desktop-Keyboard-Shortcuts.ico new file mode 100644 index 0000000..1daeb14 Binary files /dev/null and b/AppleWirelessKeyboard/Gnome-Preferences-Desktop-Keyboard-Shortcuts.ico differ diff --git a/AppleWirelessKeyboard/HIDImports.cs b/AppleWirelessKeyboard/HIDImports.cs new file mode 100644 index 0000000..8557fb7 --- /dev/null +++ b/AppleWirelessKeyboard/HIDImports.cs @@ -0,0 +1,192 @@ +using System; +using System.Runtime.InteropServices; +using System.IO; +using Microsoft.Win32.SafeHandles; + +namespace AppleWirelessKeyboard +{ + /// + /// Win32 import information for use with the Wiimote library + /// + class HIDImports + { + // + // Flags controlling what is included in the device information set built + // by SetupDiGetClassDevs + // + public const int DIGCF_DEFAULT = 0x00000001; // only valid with DIGCF_DEVICEINTERFACE + public const int DIGCF_PRESENT = 0x00000002; + public const int DIGCF_ALLCLASSES = 0x00000004; + public const int DIGCF_PROFILE = 0x00000008; + public const int DIGCF_DEVICEINTERFACE = 0x00000010; + + [Flags] + public enum EFileAttributes : uint + { + Readonly = 0x00000001, + Hidden = 0x00000002, + System = 0x00000004, + Directory = 0x00000010, + Archive = 0x00000020, + Device = 0x00000040, + Normal = 0x00000080, + Temporary = 0x00000100, + SparseFile = 0x00000200, + ReparsePoint = 0x00000400, + Compressed = 0x00000800, + Offline = 0x00001000, + NotContentIndexed = 0x00002000, + Encrypted = 0x00004000, + Write_Through = 0x80000000, + Overlapped = 0x40000000, + NoBuffering = 0x20000000, + RandomAccess = 0x10000000, + SequentialScan = 0x08000000, + DeleteOnClose = 0x04000000, + BackupSemantics = 0x02000000, + PosixSemantics = 0x01000000, + OpenReparsePoint = 0x00200000, + OpenNoRecall = 0x00100000, + FirstPipeInstance = 0x00080000 + } + + [StructLayout(LayoutKind.Sequential)] + public struct SP_DEVINFO_DATA + { + public uint cbSize; + public Guid ClassGuid; + public uint DevInst; + public IntPtr Reserved; + } + + [StructLayout(LayoutKind.Sequential)] + public struct SP_DEVICE_INTERFACE_DATA + { + public int cbSize; + public Guid InterfaceClassGuid; + public int Flags; + public IntPtr RESERVED; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct SP_DEVICE_INTERFACE_DETAIL_DATA + { + public UInt32 cbSize; + [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] + public string DevicePath; + } + + [StructLayout(LayoutKind.Sequential)] + public struct HIDD_ATTRIBUTES + { + public int Size; + public short VendorID; + public short ProductID; + public short VersionNumber; + } + + [DllImport(@"hid.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern void HidD_GetHidGuid(out Guid gHid); + + [DllImport("hid.dll")] + public static extern Boolean HidD_GetAttributes(IntPtr HidDeviceObject, ref HIDD_ATTRIBUTES Attributes); + + [DllImport("hid.dll")] + internal extern static bool HidD_SetOutputReport( + IntPtr HidDeviceObject, + byte[] lpReportBuffer, + uint ReportBufferLength); + + [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr SetupDiGetClassDevs( + ref Guid ClassGuid, + [MarshalAs(UnmanagedType.LPTStr)] string Enumerator, + IntPtr hwndParent, + UInt32 Flags + ); + + [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern Boolean SetupDiEnumDeviceInterfaces( + IntPtr hDevInfo, + //ref SP_DEVINFO_DATA devInfo, + IntPtr devInvo, + ref Guid interfaceClassGuid, + Int32 memberIndex, + ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData + ); + + [DllImport(@"setupapi.dll", SetLastError = true)] + public static extern Boolean SetupDiGetDeviceInterfaceDetail( + IntPtr hDevInfo, + ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, + IntPtr deviceInterfaceDetailData, + UInt32 deviceInterfaceDetailDataSize, + out UInt32 requiredSize, + IntPtr deviceInfoData + ); + + [DllImport(@"setupapi.dll", SetLastError = true)] + public static extern Boolean SetupDiGetDeviceInterfaceDetail( + IntPtr hDevInfo, + ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, + ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData, + UInt32 deviceInterfaceDetailDataSize, + out UInt32 requiredSize, + IntPtr deviceInfoData + ); + + [DllImport(@"setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern UInt16 SetupDiDestroyDeviceInfoList(IntPtr hDevInfo); + + [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] + public static extern SafeFileHandle CreateFile( + string fileName, + [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess, + [MarshalAs(UnmanagedType.U4)] FileShare fileShare, + IntPtr securityAttributes, + [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, + [MarshalAs(UnmanagedType.U4)] EFileAttributes flags, + IntPtr template); + + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool CloseHandle(IntPtr hObject); + + [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern bool SetupDiGetDeviceRegistryProperty( + IntPtr DeviceInfoSet, + ref SP_DEVINFO_DATA DeviceInfoData, + uint Property, + out UInt32 PropertyRegDataType, + IntPtr PropertyBuffer, + uint PropertyBufferSize, + out UInt32 RequiredSize); + + public enum SPDRP + { + SPDRP_DEVICEDESC = 0x00000000, + SPDRP_HARDWAREID = 0x00000001, + SPDRP_COMPATIBLEIDS = 0x00000002, + SPDRP_NTDEVICEPATHS = 0x00000003, + SPDRP_SERVICE = 0x00000004, + SPDRP_CONFIGURATION = 0x00000005, + SPDRP_CONFIGURATIONVECTOR = 0x00000006, + SPDRP_CLASS = 0x00000007, + SPDRP_CLASSGUID = 0x00000008, + SPDRP_DRIVER = 0x00000009, + SPDRP_CONFIGFLAGS = 0x0000000A, + SPDRP_MFG = 0x0000000B, + SPDRP_FRIENDLYNAME = 0x0000000C, + SPDRP_LOCATION_INFORMATION = 0x0000000D, + SPDRP_PHYSICAL_DEVICE_OBJECT_NAME = 0x0000000E, + SPDRP_CAPABILITIES = 0x0000000F, + SPDRP_UI_NUMBER = 0x00000010, + SPDRP_UPPERFILTERS = 0x00000011, + SPDRP_LOWERFILTERS = 0x00000012, + SPDRP_MAXIMUM_PROPERTY = 0x00000013, + } + + [DllImport("setupapi.dll", SetLastError = true)] + public static extern bool SetupDiEnumDeviceInfo(IntPtr DeviceInfoSet, uint MemberIndex, ref SP_DEVINFO_DATA DeviceInfoData); + } +} diff --git a/AppleWirelessKeyboard/IoControl.cs b/AppleWirelessKeyboard/IoControl.cs new file mode 100644 index 0000000..cff6161 --- /dev/null +++ b/AppleWirelessKeyboard/IoControl.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; +using System.IO; +using System.Threading.Tasks; + +namespace AppleWirelessKeyboard +{ + public static class IoControl + { + #region DeviceIoControl + const int OPEN_EXISTING = 3; + const uint GENERIC_READ = 0x80000000; + const uint GENERIC_WRITE = 0x40000000; + const uint FILE_SHARE_WRITE = 0x00000002; + const uint IOCTL_STORAGE_EJECT_MEDIA = 2967560; + + [DllImport("kernel32")] + private static extern IntPtr CreateFile + (string filename, uint desiredAccess, + uint shareMode, IntPtr securityAttributes, + int creationDisposition, int flagsAndAttributes, + IntPtr templateFile); + + [DllImport("kernel32")] + private static extern int DeviceIoControl + (IntPtr deviceHandle, uint ioControlCode, + IntPtr inBuffer, int inBufferSize, + IntPtr outBuffer, int outBufferSize, + ref int bytesReturned, IntPtr overlapped); + + [DllImport("kernel32")] + private static extern int CloseHandle(IntPtr handle); + #endregion + + public static void EjectMedia(char driveLetter) + { + try + { + string path = "\\\\.\\" + driveLetter + ":"; + IntPtr handle = CreateFile(path, GENERIC_READ, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); + if ((long)handle == -1) + { + throw new IOException("Unable to open drive " + driveLetter); + } + int dummy = 0; + DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, IntPtr.Zero, 0, IntPtr.Zero, 0, ref dummy, IntPtr.Zero); + CloseHandle(handle); + } + catch { } + } + public static void EjectAllMedia() + { + Task.Factory.StartNew(() => + { + NotificationCenter.NotifyEject(); + foreach (DriveInfo drive in DriveInfo.GetDrives()) + if (drive.DriveType == DriveType.CDRom) + { + EjectMedia(drive.Name[0]); + } + }); + } + } +} diff --git a/AppleWirelessKeyboard/KeyboardControl.cs b/AppleWirelessKeyboard/KeyboardControl.cs new file mode 100644 index 0000000..4baff7e --- /dev/null +++ b/AppleWirelessKeyboard/KeyboardControl.cs @@ -0,0 +1,172 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; +using System.Diagnostics; +using System.Windows.Input; +using System.Threading; +using System.Threading.Tasks; + +namespace AppleWirelessKeyboard +{ + public static class KeyboardControl + { + const int VK_SNAPSHOT = 44; + const int VK_DELETE = 46; + const int VK_MEDIA_NEXT_TRACK = 176; + const int VK_MEDIA_PREV_TRACK = 177; + const int VK_MEDIA_STOP = 178; + const int VK_MEDIA_PLAY_PAUSE = 179; + const int VK_END = 37; + const int VK_HOME = 36; + const int VK_PAGEUP = 33; + const int VK_PAGEDOWN = 35; + const int VK_F3 = 117; + const int VK_INSERT = 45; + + public static void Send(int VKey, KeyboardEvent e = KeyboardEvent.Both) + { + switch (e) + { + case KeyboardEvent.Both: + { + keybd_event((byte)VKey, 0, 0, 0); + keybd_event((byte)VKey, 0, 2, 0); + } + break; + case KeyboardEvent.Down: + keybd_event((byte)VKey, 0, 0, 0); + break; + case KeyboardEvent.Up: + keybd_event((byte)VKey, 0, 2, 0); + break; + } + } + + public static void SendInsert() + { + Task.Factory.StartNew(() => + { + Send(VK_INSERT); + }); + } + + public static void SendDelete() + { + Task.Factory.StartNew(() => + { + Send(VK_DELETE); + }); + } + + public static void SendPrintScreen() + { + Task.Factory.StartNew(() => + { + Send(VK_SNAPSHOT); + Thread.Sleep(100); + NotificationCenter.NotifyPrintScreen(); + }); + } + + public static void SendPlayPause() + { + Task.Factory.StartNew(() => + { + if (iTunesControl.iTunesRunning) + iTunesControl.PlayPause(); + else + Send(VK_MEDIA_PLAY_PAUSE); + NotificationCenter.NotifyPlayPause(); + }); + } + + public static void SendNextTrack() + { + Task.Factory.StartNew(() => + { + if (iTunesControl.iTunesRunning) + iTunesControl.NextSong(); + else + Send(VK_MEDIA_NEXT_TRACK); + NotificationCenter.NotifyNext(); + }); + } + + public static void SendPreviousTrack() + { + Task.Factory.StartNew(() => + { + if (iTunesControl.iTunesRunning) + iTunesControl.PreviousSong(); + else + Send(VK_MEDIA_PREV_TRACK); + NotificationCenter.NotifyPrevious(); + }); + } + + public static void OpenTaskManager() + { + Task.Factory.StartNew(() => + { + Process taskmgr = Process.GetProcessesByName("taskmgr.exe").FirstOrDefault(); + if (taskmgr != null) + SetForegroundWindow(taskmgr.MainWindowHandle); + else + Process.Start("taskmgr.exe"); + NotificationCenter.NotifyTaskManager(); + }); + } + + #region PInvoke + [DllImport("user32.dll")] + static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo); + + public enum KeyboardEvent + { + Down = 0, Up = 2, Both + } + #endregion + + public static void SendPageUp() + { + Task.Factory.StartNew(() => + { + Send(VK_PAGEUP); + }); + } + public static void SendPageDown() + { + Task.Factory.StartNew(() => + { + Send(VK_PAGEDOWN); + }); + } + public static void SendHome() + { + Task.Factory.StartNew(() => + { + Send(VK_HOME); + }); + } + public static void SendEnd() + { + Task.Factory.StartNew(() => + { + Send(VK_END); + }); + } + + public static void SendF3() + { + Task.Factory.StartNew(() => + { + Send(VK_F3); + }); + } + } +} diff --git a/AppleWirelessKeyboard/KeyboardHandler.cs b/AppleWirelessKeyboard/KeyboardHandler.cs new file mode 100644 index 0000000..8686f30 --- /dev/null +++ b/AppleWirelessKeyboard/KeyboardHandler.cs @@ -0,0 +1,168 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows.Input; +using System.Threading.Tasks; +using System.Threading; +using System.Windows; + +namespace AppleWirelessKeyboard +{ + public static class KeyboardHandler + { + static KeyboardHandler() + { + PowerButtonPressCount = 0; + } + public static bool FMode + { + get + { + return AppleWirelessKeyboard.Properties.Settings.Default.FMode; + } + set + { + if (AppleWirelessKeyboard.Properties.Settings.Default.FMode != value) + { + AppleWirelessKeyboard.Properties.Settings.Default.FMode = value; + if (value) + NotificationCenter.NotifyOn(); + else NotificationCenter.NotifyOff(); + } + } + } + + public static void HandleEject() + { + if (!AppleKeyboardHID2.FnDown) + FMode = !FMode; + else IoControl.EjectAllMedia(); + } + + public static bool HandleKeyDown(KeyboardListener.KeyHookEventArgs e) + { + bool F = (FMode || e.ModifierFn) && !e.ModifierAnyNative; + + switch (e.Key) + { + case Key.F3: + if (F) + { + KeyboardControl.SendPrintScreen(); + return true; + } + break; + case Key.F4: + if (F) + { + KeyboardControl.OpenTaskManager(); + return true; + } + break; + case Key.F2: + if (F) + { + KeyboardControl.SendF3(); + return true; + } + break; + case Key.F7: + if (F) + { + KeyboardControl.SendPreviousTrack(); + return true; + } + break; + case Key.F8: + if (F) + { + KeyboardControl.SendPlayPause(); + return true; + } + break; + case Key.F9: + if (F) + { + KeyboardControl.SendNextTrack(); + return true; + } + break; + case Key.F10: + if (F) + { + VolumeControl.ToggleMute(); + return true; + } + break; + case Key.F11: + if (F) + { + VolumeControl.VolumeDown(); + return true; + } + break; + case Key.F12: + if (F) + { + VolumeControl.VolumeUp(); + return true; + } + break; + case Key.Back: + if (e.ModifierFn) + { + KeyboardControl.SendDelete(); + return true; + } + break; + case Key.Up: + if (e.ModifierFn) + { + KeyboardControl.SendPageUp(); + return true; + } + break; + case Key.Down: + if (e.ModifierFn) + { + KeyboardControl.SendPageDown(); + return true; + } + break; + case Key.Left: + if(e.ModifierFn) + { + KeyboardControl.SendHome(); + return true; + } + break; + case Key.Right: + if (e.ModifierFn) + { + KeyboardControl.SendEnd(); + return true; + } + break; + case Key.Enter: + if (e.ModifierFn) + { + KeyboardControl.SendInsert(); + return true; + } + break; + } + + if (e.ModifierAnyAlt && (FMode || e.ModifierFn)) + if (e.Key == Key.F3) + { + KeyboardControl.SendPrintScreen(); + return true; + } + + return false; + } + public static int PowerButtonPressCount { get; set; } + + } +} diff --git a/AppleWirelessKeyboard/KeyboardListener.cs b/AppleWirelessKeyboard/KeyboardListener.cs new file mode 100644 index 0000000..5ec7d26 --- /dev/null +++ b/AppleWirelessKeyboard/KeyboardListener.cs @@ -0,0 +1,210 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Runtime.InteropServices; +using System.Windows.Input; +using System.Diagnostics; +using System.Windows; + +namespace AppleWirelessKeyboard +{ + /// + /// A class that manages a global low level keyboard hook + /// + public static class KeyboardListener + { + static KeyboardListener() + { + HookedKeys = new List(); + Hook = IntPtr.Zero; + } + #region PInvoke Structures + public delegate int keyboardHookProc(int code, int wParam, ref keyboardHookStruct lParam); + + public struct keyboardHookStruct + { + public int vkCode; + public int scanCode; + public int flags; + public int time; + public int dwExtraInfo; + } + + private const int WH_KEYBOARD_LL = 13; + private const int WM_KEYDOWN = 0x100; + private const int WM_KEYUP = 0x101; + private const int WM_SYSKEYDOWN = 0x104; + private const int WM_SYSKEYUP = 0x105; + #endregion + + #region Public declarations + + public static List HookedKeys { get; set; } + internal static IntPtr Hook { get; set; } + + public static event KeyHookEventHandler KeyDown; + public static event KeyHookEventHandler KeyUp; + + public static bool ModifierLeftAlt { get; set; } + public static bool ModifierRightAlt { get; set; } + public static bool ModifierLeftCtrl { get; set; } + public static bool ModifierRightCtrl { get; set; } + public static bool ModifierLeftShift { get; set; } + public static bool ModifierRightShift { get; set; } + public static bool ModifierLeftWin { get; set; } + public static bool ModifierRightWin { get; set; } + public static bool ModifierCapsLock { get; set; } + public static bool ModifierFn { get; set; } + + public class KeyHookEventArgs + { + public KeyHookEventArgs() + { + } + public Key Key { get; set; } + public bool ModifierLeftAlt { get; set; } + public bool ModifierRightAlt { get; set; } + public bool ModifierLeftCtrl { get; set; } + public bool ModifierRightCtrl { get; set; } + public bool ModifierLeftShift { get; set; } + public bool ModifierRightShift { get; set; } + public bool ModifierLeftWin { get; set; } + public bool ModifierRightWin { get; set; } + public bool ModifierCapsLock { get; set; } + public bool ModifierFn { get; set; } + + public bool ModifierAnyAlt { get { return ModifierLeftAlt || ModifierRightAlt; } set { ModifierLeftAlt = value; } } + public bool ModifierAnyCtrl { get { return ModifierLeftCtrl || ModifierRightCtrl; } set { ModifierLeftCtrl = value; } } + public bool ModifierAnyShift { get { return ModifierLeftShift || ModifierRightShift; } set { ModifierLeftShift = value; } } + public bool ModifierAnyWin { get { return ModifierLeftWin || ModifierRightWin; } set { ModifierLeftWin = value; } } + + public bool ModifierAnyNative { get { return (((ModifierAnyAlt) || (ModifierAnyWin)) || ((ModifierAnyCtrl) || (ModifierAnyShift))); } } + public bool ModifierAny { get { return ((ModifierAny) || (ModifierFn)); } } + } + + public delegate bool KeyHookEventHandler(KeyHookEventArgs e); + #endregion + + #region Public Methods + + internal static void SetModifiers(Key key, bool IsPressed, int VKey) + { + switch (key) + { + case Key.LeftAlt: + ModifierLeftAlt = IsPressed; + break; + case Key.RightAlt: + ModifierRightAlt = IsPressed; + break; + case Key.RightCtrl: + ModifierRightCtrl = IsPressed; + break; + case Key.LeftCtrl: + ModifierLeftCtrl = IsPressed; + break; + case Key.LeftShift: + ModifierLeftShift = IsPressed; + break; + case Key.RightShift: + ModifierRightShift = IsPressed; + break; + case Key.CapsLock: + ModifierCapsLock = IsPressed; + break; + case Key.LWin: + ModifierLeftWin = IsPressed; + break; + case Key.RWin: + ModifierRightWin = IsPressed; + break; + } + if (!AppleKeyboardHID2.Registered) + AppleKeyboardHID2.Start(); + } + internal static KeyHookEventArgs CreateEventArgs(Key key) + { + return new KeyHookEventArgs() + { + Key = key, + ModifierCapsLock = ModifierCapsLock, + ModifierLeftAlt = ModifierLeftAlt, + ModifierRightAlt = ModifierRightAlt, + ModifierLeftCtrl = ModifierLeftCtrl, + ModifierRightCtrl = ModifierRightCtrl, + ModifierLeftShift = ModifierLeftShift, + ModifierRightShift = ModifierRightShift, + ModifierLeftWin = ModifierLeftWin, + ModifierRightWin = ModifierRightWin, + ModifierFn = ModifierFn + }; + } + + public static keyboardHookProc HookProcessor { get; set; } + + public static void Register() + { + HookProcessor = Hook_Callback; + IntPtr hInstance = GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); + Hook = SetWindowsHookEx(WH_KEYBOARD_LL, HookProcessor, hInstance, 0); + } + + public static void UnRegister() + { + UnhookWindowsHookEx(Hook); + } + + public static bool TriggerKeyDown(KeyHookEventArgs args) + { + if (KeyDown != null) + return KeyDown(args); + else return false; + } + public static bool TriggerKeyUp(KeyHookEventArgs args) + { + if (KeyUp != null) + return KeyUp(args); + else return false; + } + public static int Hook_Callback(int code, int wParam, ref keyboardHookStruct lParam) + { + if (code >= 0) + { + Key key = (Key)System.Windows.Input.KeyInterop.KeyFromVirtualKey(lParam.vkCode); + bool IsPressed = (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN); + + SetModifiers(key, IsPressed, lParam.vkCode); + if (HookedKeys.Contains(key)) + { + KeyHookEventArgs kea = CreateEventArgs(key); + + bool Handled = false; + if (IsPressed) + Handled = TriggerKeyDown(kea); + else + Handled = TriggerKeyUp(kea); + + if (Handled) + return 1; + } + } + return CallNextHookEx(Hook, code, wParam, ref lParam); + } + #endregion + + #region DLL imports + [DllImport("user32.dll")] + static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId); + + [DllImport("user32.dll")] + static extern bool UnhookWindowsHookEx(IntPtr hInstance); + + [DllImport("user32.dll")] + static extern int CallNextHookEx(IntPtr idHook, int nCode, int wParam, ref keyboardHookStruct lParam); + + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern IntPtr GetModuleHandle(string lpModuleName); + + #endregion + } +} diff --git a/AppleWirelessKeyboard/MainWindow.xaml b/AppleWirelessKeyboard/MainWindow.xaml new file mode 100644 index 0000000..6f6d9b0 --- /dev/null +++ b/AppleWirelessKeyboard/MainWindow.xaml @@ -0,0 +1,15 @@ + + + + + + + + + + + diff --git a/AppleWirelessKeyboard/MainWindow.xaml.cs b/AppleWirelessKeyboard/MainWindow.xaml.cs new file mode 100644 index 0000000..28760de --- /dev/null +++ b/AppleWirelessKeyboard/MainWindow.xaml.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using System.Windows.Media.Animation; + +namespace AppleWirelessKeyboard +{ + /// + /// Interaction logic for MainWindow.xaml + /// + public partial class MainWindow : Window + { + public MainWindow() + { + InitializeComponent(); + } + public void ShowOff(Control glyph, bool valueBar = false, int value = 0) + { + this.DataContext = new { Glyph = glyph }; + + if (valueBar) ValueBar.Visibility = System.Windows.Visibility.Visible; + else ValueBar.Visibility = System.Windows.Visibility.Collapsed; + + MakeValue(value); + + this.Show(); + + DoubleAnimationUsingKeyFrames Fade = new DoubleAnimationUsingKeyFrames(); + Fade.Duration = new Duration(TimeSpan.FromSeconds(1)); + Fade.KeyFrames.Add(new LinearDoubleKeyFrame(1, KeyTime.FromPercent(0))); + Fade.KeyFrames.Add(new LinearDoubleKeyFrame(1, KeyTime.FromPercent(0.5))); + Fade.KeyFrames.Add(new LinearDoubleKeyFrame(0, KeyTime.FromPercent(1))); + BeginAnimation(OpacityProperty, Fade); + } + public void MakeValue(int value) + { + ValueBar.Children.Clear(); + + for (int i = 0; i <= value; i++) + { + Rectangle rect = new Rectangle(); + rect.Fill = new SolidColorBrush(Colors.White); + rect.Width = 6; + rect.Height = 6; + rect.Margin = new Thickness(0, 0, 3, 0); + ValueBar.Children.Add(rect); + } + } + } +} diff --git a/AppleWirelessKeyboard/NotificationCenter.cs b/AppleWirelessKeyboard/NotificationCenter.cs new file mode 100644 index 0000000..6edc57c --- /dev/null +++ b/AppleWirelessKeyboard/NotificationCenter.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace AppleWirelessKeyboard +{ + public static class NotificationCenter + { + static NotificationCenter() + { + + } + public static void NotifyMuteOn() + { + App.Window.Dispatcher.Invoke(new Action(delegate + { + App.Window.ShowOff(new Glyphs.MuteOn()); + })); + } + public static void NotifyMuteOff() + { + App.Window.Dispatcher.Invoke(new Action(delegate + { + App.Window.ShowOff(new Glyphs.MuteOff()); + })); + } + public static void NotifyNoVolume() + { + App.Window.Dispatcher.Invoke(new Action(delegate + { + App.Window.ShowOff(new Glyphs.VolumeOff()); + })); + } + public static void NotifyVolumeLevel(int level)//1-16 + { + App.Window.Dispatcher.Invoke(new Action(delegate + { + App.Window.ShowOff(new Glyphs.VolumeOn(), true, level); + })); + } + public static void NotifyTaskManager() + { + App.Window.Dispatcher.Invoke((Action)delegate + { + App.Window.ShowOff(new Glyphs.TaskManager()); + }); + } + public static void NotifyPrintScreen() + { + App.Window.Dispatcher.Invoke((Action)delegate + { + App.Window.ShowOff(new Glyphs.PrintScreen()); + }); + } + public static void NotifyOn() + { + App.Window.Dispatcher.Invoke((Action)delegate + { + App.Window.ShowOff(new Glyphs.On()); + }); + } + public static void NotifyOff() + { + App.Window.Dispatcher.Invoke((Action)delegate + { + App.Window.ShowOff(new Glyphs.Off()); + }); + } + public static void NotifyPlayPause() + { + App.Window.Dispatcher.Invoke((Action)delegate + { + App.Window.ShowOff(new Glyphs.PlayPause()); + }); + } + public static void NotifyPrevious() + { + App.Window.Dispatcher.Invoke((Action)delegate + { + App.Window.ShowOff(new Glyphs.Previous()); + }); + } + public static void NotifyNext() + { + App.Window.Dispatcher.Invoke((Action)delegate + { + App.Window.ShowOff(new Glyphs.Next()); + }); + } + + internal static void NotifyEject() + { + App.Window.Dispatcher.Invoke((Action)delegate + { + App.Window.ShowOff(new Glyphs.Eject()); + }); + } + } +} diff --git a/AppleWirelessKeyboard/PowerControl.cs b/AppleWirelessKeyboard/PowerControl.cs new file mode 100644 index 0000000..7d2739c --- /dev/null +++ b/AppleWirelessKeyboard/PowerControl.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; +using System.Diagnostics; +using System.Threading.Tasks; +using System.Threading; +using System.Windows; + +namespace AppleWirelessKeyboard +{ + public class PowerControl + { + public static void Hibernate() + { + Task.Factory.StartNew(() => + { + if (PowerStatusBox.PowerAction("Hibernate", 10)) + SetSuspendState(true, true, true); + }); + } + + public static void Shutdown() + { + Task.Factory.StartNew(() => + { + if (PowerStatusBox.PowerAction("Shut Down", 10)) + { + ProcessStartInfo si = new ProcessStartInfo("shutdown", "/s /t 0"); + si.CreateNoWindow = true; + si.WindowStyle = ProcessWindowStyle.Hidden; + Process.Start(si); + } + }); + } + + [DllImport("powrprof.dll", SetLastError = true)] + static extern bool SetSuspendState(bool hibernate, bool forceCritical, bool disableWakeEvent); + } +} diff --git a/AppleWirelessKeyboard/PowerStatusBox.xaml b/AppleWirelessKeyboard/PowerStatusBox.xaml new file mode 100644 index 0000000..b3a7c45 --- /dev/null +++ b/AppleWirelessKeyboard/PowerStatusBox.xaml @@ -0,0 +1,16 @@ + + + + + + + + +