Exemple #1
0
 private void UnRegisterTriggers(Profile profile)
 {
     foreach (var trigger in profile.Triggers)
     {
         trigger.Type.Switch(() =>
         {
             WindowsAPIAdapter.UnRegisterHotKey(trigger.HotKey);
             _profilesByHotkey.Remove(trigger.HotKey);
         },
                             () => { _profilesByWindowName.Remove(trigger.WindowName.ToLower()); },
                             () => { _profileByApplication.Remove(trigger.ApplicationPath.ToLower()); },
                             () => { _steamProfile = null; }, () => { });
     }
 }
        public Result <ProfileSetting[], VoidSuccess> Init()
        {
            RegisterEvents();

            var errors = AppConfigs.Configuration.ProfileSettings
                         .Where(setting => setting.HotKey != null)
                         .Where(profileSetting => !WindowsAPIAdapter.RegisterHotKey(profileSetting.HotKey))
                         .ToArray();

            InitializeProfileExistingProcess();

            if (errors.Length > 0)
            {
                return(errors);
            }

            return(Result.Success());
        }
Exemple #3
0
        public Result <Profile[], VoidSuccess> Init()
        {
            foreach (var profile in AppConfigs.Configuration.Profiles)
            {
                RegisterTriggers(profile, true);
            }
            RegisterEvents();

            var errors = _profilesByHotkey
                         .Where(pair => !WindowsAPIAdapter.RegisterHotKey(pair.Key))
                         .Select(pair => pair.Value)
                         .ToArray();

            InitializeProfileExistingProcess();

            if (errors.Length > 0)
            {
                return(errors);
            }

            return(Result.Success());
        }
        private static void Main()
        {
            InitializeLogger();
            Log.Information("Application Starts");
#if !DEBUG
            AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
            {
                HandleException((Exception)args.ExceptionObject);
            };

            Log.Information("Set Exception Handler");
            Application.ThreadException += Application_ThreadException;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            WindowsAPIAdapter.Start(Application_ThreadException);
#else
            WindowsAPIAdapter.Start();
#endif
            Thread.CurrentThread.CurrentUICulture = new LanguageFactory().Get(AppModel.Instance.Language).CultureInfo;
            Thread.CurrentThread.Name             = "Main Thread";
            var userMutexName = Application.ProductName + Environment.UserName;

            using var mainMutex = new Mutex(true, Application.ProductName);
            using var userMutex = new Mutex(true, userMutexName, out var userMutexInUse);

            if (KillOtherInstanceAndRestart(userMutexName, userMutexInUse))
            {
                return;
            }


            SetProcessDPIAware();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // Manage the Closing events send by Windows
            // Since this app don't use a Form as "main window" the app doesn't close
            // when it should without this.
            WindowsAPIAdapter.RestartManagerTriggered += (sender, @event) =>
            {
                Log.Debug("Restart Event received: {Event}", @event);
                switch (@event.Type)
                {
                case WindowsAPIAdapter.RestartManagerEventType.Query:
                    @event.Result = new IntPtr(1);

                    break;

                case WindowsAPIAdapter.RestartManagerEventType.EndSession:
                case WindowsAPIAdapter.RestartManagerEventType.ForceClose:
                    Log.Debug("Close Application");
                    Application.Exit();
                    break;
                }
            };

            Log.Information("Set Tray Icon with Main");
#if !DEBUG
            try
            {
#endif
            MMNotificationClient.Instance.Register();


            using var ctx  = new WindowsFormsSynchronizationContext();
            using var icon = new TrayIcon();

            SynchronizationContext.SetSynchronizationContext(ctx);
            try
            {
                ctx.Post(async iconState => await InitAsync((TrayIcon)iconState), icon);
                Application.Run();
            }
            finally
            {
                SynchronizationContext.SetSynchronizationContext(null);
            }


#if !DEBUG
        }

        catch (Exception ex)
        {
            HandleException(ex);
        }
#endif


            AppModel.Instance.ActiveAudioDeviceLister.Dispose();
            AppModel.Instance.ActiveUnpluggedAudioLister.Dispose();
            MMNotificationClient.Instance.UnRegister();
            WindowsAPIAdapter.Stop();
            Log.CloseAndFlush();
        }