예제 #1
0
        private async Task ExecuteProcessPresets(ProcessMonitorContext context)
        {
            var selectedDevice = SelectedDevice;

            var presets = _presets.Where(p => p.Triggers.Any(t => t.Trigger == PresetTriggerType.ProcessSwitch) &&
                                         ((string.IsNullOrEmpty(p.DeviceMacAddress) && selectedDevice?.TriggersEnabled == true) ||
                                          Devices.Any(d => d.TriggersEnabled && p.DeviceMacAddress.Equals(d.MacAddress, StringComparison.OrdinalIgnoreCase))));

            if (!presets.Any())
            {
                return;
            }

            context.IsNotificationDisabled = FormUtils.IsNotificationDisabled();

            var(processId, isFullScreen) = FormUtils.GetForegroundProcessIdAndIfFullScreen();

            if (processId == Environment.ProcessId)
            {
                return;
            }

            if (processId > 0)
            {
                var process = context.RunningProcesses.FirstOrDefault(p => p.Id == processId);

                if (process?.ProcessName?.Contains(".scr") ?? false)
                {
                    return;
                }

                context.ForegroundProcess             = process;
                context.ForegroundProcessIsFullScreen = isFullScreen;
            }
            else
            {
                context.ForegroundProcess             = null;
                context.ForegroundProcessIsFullScreen = false;
            }

            var changedProcesses = new List <Process>();

            if (context.ForegroundProcess != null)
            {
                changedProcesses.Add(context.ForegroundProcess);
            }

            if (context.ForegroundProcess != null && context.ForegroundProcessIsFullScreen)
            {
                if (!context.LastFullScreenProcessName.Equals(context.ForegroundProcess.ProcessName))
                {
                    context.LastFullScreenProcessName = context.ForegroundProcess.ProcessName;
                    //Logger.Debug($"Foreground fullscreen app detected: {context.ForegroundProcess.ProcessName}");
                }

                presets = presets.Where(p => p.Triggers.Any(t => t.Conditions.HasFlag(PresetConditionType.FullScreen)));
                context.WasFullScreen = true;
            }
            else if (context.WasFullScreen)
            {
                presets = presets.Where(p => p.Triggers.Any(t => !t.Conditions.HasFlag(PresetConditionType.FullScreen)));
                context.WasFullScreen             = false;
                context.LastFullScreenProcessName = string.Empty;
            }

            var isHDRActive = context.Devices.Any(d => d.IsUsingHDRPictureMode());

            var isGsyncActive = LgDevice.ExternalServiceHandler("GsyncEnabled", new[] { "" });

            var triggerContext = new PresetTriggerContext
            {
                IsHDRActive                   = isHDRActive,
                IsGsyncActive                 = isGsyncActive,
                ForegroundProcess             = context.ForegroundProcess,
                ForegroundProcessIsFullScreen = context.ForegroundProcessIsFullScreen,
                ChangedProcesses              = changedProcesses,
                IsNotificationDisabled        = context.IsNotificationDisabled
            };

            var toApplyPresets = new List <LgPreset>();

            foreach (var preset in presets)
            {
                if (preset.Triggers.Any(t => t.TriggerActive(triggerContext)))
                {
                    toApplyPresets.Add(preset);
                }
            }

            if (toApplyPresets.Any())
            {
                LgPreset toApplyPreset;
                if (toApplyPresets.Count > 1)
                {
                    toApplyPreset = toApplyPresets.FirstOrDefault(p => p.Triggers.Any(t => !t.IncludedProcesses.Contains("*")));
                    toApplyPreset = toApplyPreset != null ? toApplyPreset : toApplyPresets.First();
                }
                else
                {
                    toApplyPreset = toApplyPresets.First();
                }

                if (_lastTriggeredPreset != toApplyPreset)
                {
                    await ApplyPreset(toApplyPreset);

                    _lastTriggeredPreset = toApplyPreset;
                }
            }
        }