Jump to content

Hello! 👋

These forums are now archived (read only).

Join us on Discord.

Hook into screen switching events


Recommended Posts

lordofduct

It'd be nice if there was a way to hook into the events that occur in synergy.

For example it'd be nice if we could hook into when a specific computer/screen receives/loses the mouse (basically at the same time the log signals "switch from 'pc1' to 'pc2'"). And when we hook into it we can run a batch/bash/shell script or something else like that.

For me specifically I'd like an application to get focus/maximize when I've moused to another screen, and minimize when I leave the screen. But the ability to run any batch/bash/shell script would give the freedom to really hook into the host OS in any way possible.

Just like with the 'hotkey', we could just have a different option in the hotkey being an event of some sort (exit/enter by pc name), and the actions just gets another entry in the window that runs a batch/bash/shell script. This could even integrate with hotkeys too letting us also use the hotkeys to run scripts as well.

And maybe other events? Like server stop/start, client connect/disconnect, etc.

Personally I'd only need them on the server... but it'd be cool if you could also hook into these events on clients as well (well, the events that make sense from the client side... exit/enter would, but like server stop/start isn't so much).

Edited by lordofduct
Link to post
Share on other sites
lordofduct

So in the mean time I came up with a hack way to get what I want.

Since my server is a windows machine I wrote a simple service that monitors my log file. When it changes I grab the new text and process it. If the lines include the messages for a switch between computers, I grab out the from and to computers, and perform an action based on it:

	using System;
using System.IO;
using System.ServiceProcess;
using System.Text.RegularExpressions;
	namespace SynergyScreenChangeMonitor
{
    public partial class MonitorService : ServiceBase
    {
	        #region Fields
	        private FileSystemWatcher _watcher;
        private long _position;
	        #endregion
	        #region CONSTRUCTOR
	        public MonitorService()
        {
            InitializeComponent();
        }
	        #endregion
	        #region Properties
	        public string FileToMonitor
        {
            get;
            set;
        }
	        #endregion
	        #region Methods
	        protected override void OnStart(string[] args)
        {
            if(_watcher != null)
            {
                this.OnStop();
            }
	            if (!File.Exists(this.FileToMonitor)) return;
	            using (var strm = new FileStream(this.FileToMonitor, FileMode.Open))
            {
                _position = strm.Length - 1;
            }
	            // Create a new FileSystemWatcher and set its properties.
            _watcher = new FileSystemWatcher();
            _watcher.Path = Path.GetDirectoryName(this.FileToMonitor);
            /* Watch for changes in LastAccess and LastWrite times, and 
               the renaming of files or directories. */
            _watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.DirectoryName;
            // Only watch text files.
            _watcher.Filter = Path.GetFileName(this.FileToMonitor);
	            // Add event handlers.
            _watcher.Changed += OnChanged;
            _watcher.Created += OnChanged;
            _watcher.Deleted += OnChanged;
	            // Begin watching.
            _watcher.EnableRaisingEvents = true;
        }
	        protected override void OnStop()
        {
            if(_watcher != null)
            {
                _watcher.Dispose();
                _watcher = null;
            }
        }
	        private void OnChanged(object sender, FileSystemEventArgs e)
        {
            switch(e.ChangeType)
            {
                case WatcherChangeTypes.Created:
                case WatcherChangeTypes.Deleted:
                    _position = 0;
                    break;
                case WatcherChangeTypes.Changed:
                    string txt = string.Empty;
                    try
                    {
                        using (FileStream stream = File.Open(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        using (var reader = new StreamReader(stream))
                        {
                            reader.BaseStream.Position = Math.Min(_position, reader.BaseStream.Length - 1);
                            txt = reader.ReadToEnd();
                            _position = reader.BaseStream.Position;
                        }
                    }
                    catch (Exception) { }
	                    this.ProcessNewLines(txt ?? string.Empty);
                    break;
            }
        }
	        private void ProcessNewLines(string txt)
        {
            using (var reader = new StringReader(txt))
            {
                string ln;
                while((ln = reader.ReadLine()) != null)
                {
                    if (string.IsNullOrWhiteSpace(ln)) continue;
	                    var m = Regex.Match(ln, @"\[.*\] INFO\: switch from ""(?<from>.*)"" to ""(?<to>.*)"" at");
                    if(m.Success)
                    {
                        string from = m.Groups["from"].Value;
                        string to = m.Groups["to"].Value;
                        //DO STUFF - based on from/to
                    }
                }
            }
        }
	        internal void DebugStart()
        {
            OnStart(null);
        }
	        #endregion
	    }
}
	

Link to post
Share on other sites
  • 1 month later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...