bool RegisterExplorerExit(Process[] procs = null) { if (Taskmaster.Trace) { Log.Verbose("<Tray> Registering Explorer crash monitor."); } // this is for dealing with notify icon disappearing on explorer.exe crash/restart if (procs == null || procs.Length == 0) { procs = ExplorerInstances; } if (procs.Length > 0) { Explorer = procs; foreach (var proc in procs) { var info = ProcessManagerUtility.GetInfo(proc.Id, process: proc, name: "explorer", getPath: true); if (info == null) { continue; // things failed, move on } if (!string.IsNullOrEmpty(info.Path)) { if (!info.Path.StartsWith(Environment.GetFolderPath(Environment.SpecialFolder.Windows), StringComparison.InvariantCultureIgnoreCase)) { if (Taskmaster.Trace) { Log.Verbose("<Tray> Explorer (#{Pid}) not in system root.", info.Id); } continue; } } bool added = false; lock (explorer_lock) { added = KnownExplorerInstances.Add(info.Id); } if (added) { proc.Exited += (s, e) => { ExplorerCrashHandler(info.Id); }; proc.EnableRaisingEvents = true; } Log.Information("<Tray> Explorer (#{ExplorerProcessID}) registered.", info.Id); } return(true); } Log.Warning("<Tray> Explorer not found."); return(false); }
private void OnSessionCreated(object sender, NAudio.CoreAudioApi.Interfaces.IAudioSessionControl asession) { Debug.Assert(System.Threading.Thread.CurrentThread != Context, "Must be called in same thread."); try { var session = new NAudio.CoreAudioApi.AudioSessionControl(asession); int pid = (int)session.GetProcessID; string name = session.DisplayName; float volume = session.SimpleAudioVolume.Volume; var info = ProcessManagerUtility.GetInfo(pid, getPath: true); if (info != null) { //OnNewSession?.Invoke(this, info); var prc = Taskmaster.processmanager.getController(info.Name); if (prc == null && !string.IsNullOrEmpty(info.Path)) { prc = Taskmaster.processmanager.getWatchedPath(info); } if (prc != null) { bool volAdjusted = false; float oldvolume = session.SimpleAudioVolume.Volume; switch (prc.VolumeStrategy) { default: case AudioVolumeStrategy.Ignore: break; case AudioVolumeStrategy.Force: session.SimpleAudioVolume.Volume = prc.Volume; volAdjusted = true; break; case AudioVolumeStrategy.Decrease: if (session.SimpleAudioVolume.Volume > prc.Volume) { session.SimpleAudioVolume.Volume = prc.Volume; volAdjusted = true; } break; case AudioVolumeStrategy.Increase: if (session.SimpleAudioVolume.Volume < prc.Volume) { session.SimpleAudioVolume.Volume = prc.Volume; volAdjusted = true; } break; } if (volAdjusted) { Serilog.Log.Information("<Audio> {Process} (#{Pid}) volume changed from {Source} to {Target}", info.Name, info.Id, string.Format("{0:N1}%", oldvolume * 100), string.Format("{0:N1}%", prc.Volume * 100)); } else { if (Taskmaster.ShowInaction && Taskmaster.DebugAudio) { Serilog.Log.Debug("<Audio> {Name} (#{Pid}) Volume: {Volume} – Already correct (Plan: {Plan})", info.Name, pid, string.Format("{0:N1}%", volume * 100), prc.VolumeStrategy.ToString()); } } } else { if (Taskmaster.ShowInaction && Taskmaster.DebugAudio) { Serilog.Log.Debug("<Audio> {Name} (#{Pid}) Volume: {Volume} – not watched: {Path}", info.Name, pid, string.Format("{0:N1}%", volume * 100), info.Path); } } } else { Serilog.Log.Debug("<Audio> Failed to get info for session (#{Pid})", pid); } } catch (Exception ex) { Logging.Stacktrace(ex); } }