204 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
		
		
			
		
	
	
			204 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
|  | using BarRaider.SdTools; | |||
|  | using BarRaider.SdTools.Payloads; | |||
|  | using CoreAudio; | |||
|  | using Newtonsoft.Json; | |||
|  | using Newtonsoft.Json.Linq; | |||
|  | using System; | |||
|  | using System.Collections.Generic; | |||
|  | using System.Data; | |||
|  | using System.Diagnostics; | |||
|  | using System.Drawing; | |||
|  | using System.IO; | |||
|  | using System.Linq; | |||
|  | using System.Runtime.InteropServices; | |||
|  | using System.Text; | |||
|  | using System.Threading; | |||
|  | using System.Threading.Tasks; | |||
|  | 
 | |||
|  | namespace FocusVolumeControl | |||
|  | { | |||
|  | 
 | |||
|  |     /* | |||
|  |     todo: | |||
|  |     link both discord processes | |||
|  |     steam not detecting | |||
|  | 
 | |||
|  |     long press reset | |||
|  | 
 | |||
|  |     option for what to do when on app without sound | |||
|  | 
 | |||
|  |     gitea | |||
|  |      */ | |||
|  | 
 | |||
|  | 
 | |||
|  |     [PluginActionId("com.dlprows.focusvolumecontrol.dialaction")] | |||
|  |     public class DialAction : EncoderBase | |||
|  |     { | |||
|  |         private class PluginSettings | |||
|  |         { | |||
|  |             public static PluginSettings CreateDefaultSettings() | |||
|  |             { | |||
|  |                 PluginSettings instance = new PluginSettings(); | |||
|  |                 return instance; | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |         private PluginSettings settings; | |||
|  |         //IntPtr _foregroundWindowChangedEvent; | |||
|  |         //WinEventDelegate _delegate; | |||
|  |         ActiveAudioSessionWrapper _currentAudioSession; | |||
|  |         AudioHelper _audioHelper = new AudioHelper(); | |||
|  | 
 | |||
|  |         public DialAction(ISDConnection connection, InitialPayload payload) : base(connection, payload) | |||
|  |         { | |||
|  |             if (payload.Settings == null || payload.Settings.Count == 0) | |||
|  |             { | |||
|  |                 settings = PluginSettings.CreateDefaultSettings(); | |||
|  |                 SaveSettings(); | |||
|  |             } | |||
|  |             else | |||
|  |             { | |||
|  |                 settings = payload.Settings.ToObject<PluginSettings>(); | |||
|  |             } | |||
|  | 
 | |||
|  |             //_delegate = new WinEventDelegate(WinEventProc); | |||
|  |             //_foregroundWindowChangedEvent = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _delegate, 0, 0, WINEVENT_OUTOFCONTEXT); | |||
|  |         } | |||
|  | 
 | |||
|  |         public override async void DialDown(DialPayload payload) | |||
|  |         { | |||
|  |             //dial pressed down | |||
|  |             Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Down"); | |||
|  |             if(_currentAudioSession != null) | |||
|  |             { | |||
|  |                 _currentAudioSession.Volume.Mute = !_currentAudioSession.Volume.Mute; | |||
|  |                 var uiState = UIState.Build(_currentAudioSession); | |||
|  |                 await Connection.SetFeedbackAsync(uiState); | |||
|  |             } | |||
|  |             else | |||
|  |             { | |||
|  |                 await Connection.ShowAlert(); | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |         public override async void TouchPress(TouchpadPressPayload payload) | |||
|  |         { | |||
|  |             Logger.Instance.LogMessage(TracingLevel.INFO, "Touch Press"); | |||
|  |             if (payload.IsLongPress) | |||
|  |             { | |||
|  |                 //todo: iterate through all sessions setting them back to 100 except the master volume | |||
|  |             } | |||
|  |             else | |||
|  |             { | |||
|  | 
 | |||
|  | 
 | |||
|  |                 if (_currentAudioSession != null) | |||
|  |                 { | |||
|  |                     _currentAudioSession.Volume.Mute = !_currentAudioSession.Volume.Mute; | |||
|  |                     var uiState = UIState.Build(_currentAudioSession); | |||
|  |                     await Connection.SetFeedbackAsync(uiState); | |||
|  |                 } | |||
|  |                 else | |||
|  |                 { | |||
|  |                     await Connection.ShowAlert(); | |||
|  |                 } | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |         public override async void DialRotate(DialRotatePayload payload) | |||
|  |         { | |||
|  |             Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Rotate"); | |||
|  |             //dial rotated. ticks positive for right, negative for left | |||
|  |             if(_currentAudioSession != null) | |||
|  |             { | |||
|  |                 _currentAudioSession.Volume.MasterVolume += (0.01f) * payload.Ticks; | |||
|  | 
 | |||
|  |                 var uiState = UIState.Build(_currentAudioSession); | |||
|  |                 await Connection.SetFeedbackAsync(uiState); | |||
|  |             } | |||
|  |             else | |||
|  |             { | |||
|  |                 await Connection.ShowAlert(); | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |         public override void DialUp(DialPayload payload) | |||
|  |         { | |||
|  |             //dial unpressed | |||
|  |             Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Up"); | |||
|  |         } | |||
|  | 
 | |||
|  |         public override void Dispose() | |||
|  |         { | |||
|  |             /* | |||
|  |             if(_foregroundWindowChangedEvent != IntPtr.Zero) | |||
|  |             { | |||
|  |                 Native.UnhookWinEvent(_foregroundWindowChangedEvent); | |||
|  |             } | |||
|  |             */ | |||
|  |         } | |||
|  | 
 | |||
|  |         public override async void OnTick() | |||
|  |         { | |||
|  |             //called once every 1000ms and can be used for updating the title/image fo the key | |||
|  |             var activeSession = _audioHelper.GetActiveSession(); | |||
|  | 
 | |||
|  |             if (activeSession == null) | |||
|  |             { | |||
|  |                 //todo: something? | |||
|  |             } | |||
|  |             else | |||
|  |             { | |||
|  |                 _currentAudioSession = activeSession; | |||
|  |             } | |||
|  | 
 | |||
|  |             if(_currentAudioSession != null) | |||
|  |             { | |||
|  |                 var uiState = UIState.BuildWithImage(_currentAudioSession); | |||
|  |                 await Connection.SetFeedbackAsync(uiState); | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  |         public override void ReceivedGlobalSettings(ReceivedGlobalSettingsPayload payload) | |||
|  |         { | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  |         public override void ReceivedSettings(ReceivedSettingsPayload payload) | |||
|  |         { | |||
|  |             Tools.AutoPopulateSettings(settings, payload.Settings); | |||
|  |             SaveSettings(); | |||
|  |         } | |||
|  | 
 | |||
|  |         private Task SaveSettings() | |||
|  |         { | |||
|  |             return Connection.SetSettingsAsync(JObject.FromObject(settings)); | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  |         /* | |||
|  |         public async void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) | |||
|  |         { | |||
|  |             var activeSession = _audioHelper.GetActiveSession(); | |||
|  | 
 | |||
|  | 
 | |||
|  |             if(activeSession == null) | |||
|  |             { | |||
|  |                 //todo: something? | |||
|  | 
 | |||
|  |             } | |||
|  |             else | |||
|  |             { | |||
|  |                 _currentAudioSession = activeSession; | |||
|  | 
 | |||
|  |                 //populate the UI | |||
|  |                 await Connection.SetTitleAsync(_currentAudioSession.DisplayName); | |||
|  | 
 | |||
|  |             } | |||
|  |         } | |||
|  |         */ | |||
|  |     } | |||
|  | 
 | |||
|  | } |